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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

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