little_greg Posted September 11, 2009 Report Share Posted September 11, 2009 Подскажите способ как сделать так, чтобы при использовании 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;}; Quote Link to comment Share on other sites More sharing options...
Тролль Posted September 11, 2009 Report Share Posted September 11, 2009 (edited) little_greg: Используй для ввода строк с пробелами класса string вместо cin >> pAuto[i].sName; оператор getline(cin,pAuto[i].sName); Edited September 11, 2009 by Тролль Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.