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;}; 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 Тролль Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now