Jump to content

Строки в С++ (DEV C++)


Recommended Posts

Подскажите способ как сделать так, чтобы при использовании string sName программа работала нормально, а то при вводе имени с пробелом, потом больше программа ничего не вводит.

#include <iostream>#include <cstring>using namespace std;const int SIZE = 3;struct Car{   string sName;   int nYear;};int main(){Car* pAuto = new Car[size];for (int i = 0; i < SIZE; i++){	cout << "enter car name: ";	cin >> pAuto[i].sName;	cout << "enter model year: ";	cin >> pAuto[i].nYear;	cin.get();	}for (int l = 0; l < SIZE; l++){	cout << "Model name: " << pAuto[l].sName << endl		 << "Model year: " << pAuto[l].nYear << endl;		 }system("pause");return 0;};

если использовать char sName[20] и подправить несколько операторов, то все работает нормально.

#include <iostream>#include <cstring>using namespace std;const int SIZE = 3;struct Car{   char sName[20];   int nYear;};int main(){Car* pAuto = new Car[size];for (int i = 0; i < SIZE; i++){	cout << "enter car name: ";	cin.getline(pAuto[i].sName, 20);	cout << "enter model year: ";	cin >> pAuto[i].nYear;	cin.get();	}for (int l = 0; l < SIZE; l++){	cout << "Model name: " << pAuto[l].sName << endl		 << "Model year: " << pAuto[l].nYear << endl;		 }system("pause");return 0;};
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...