当我为第一个问题输入大量信息时,为什么我的代码会跳过最后一个问题?我做错了什么?
const int SIZEC =31;
char phrase[SIZEC];
cout << " Provide a phrase, up to 30 characters with spaces. > " << endl;
cin.getline(phrase, SIZEC);
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << " The phrase is: " << phrase << endl;
cout << endl;
cout << " Using sring Class Obects " << endl;
cout << "--------------------------" << endl;
cout << endl;
string leter;
cout << " Provide a single character > " << endl;
cin >> leter;
cout << " The single character is: " << leter << endl;
cout << endl;
如果需要之前的代码,请告诉我,我会添加它。
最佳答案
使用std::string::resize
作为解决方法。
string phrase;
getline(cin, phrase);
phrase.resize(30); // phrase will be reduced to 30 chars
string letter; // better to use char letter
cin >> letter;
letter.resize(1);
关于c++ - 为什么我的部分代码被跳过而不让我输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41994681/