【发布时间】:2020-03-20 17:46:07
【问题描述】:
为什么“错误:必须引入 1 或 0”消息只出现 2 次而不总是出现?
int instruction() {
string instruction;
cout << "Do you want to see the tutorial of the game? If yes press: 1, else press: 0" << endl << "--> ";
while (getline(cin,instruction)){
if (instruction == "1" || instruction == "0") {
break;
}else{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "Error: Must introduce 1 or 0" << endl << "--> ";
}
}
调试输出:
Do you want to see the tutorial of the game? If yes press: 1, else press: 0
--> asdasd
asdasdasd
Error: Must introduce 1 or 0
--> sdfasdfasdf
asdf
Error: Must introduce 1 or 0
--> sad
fasd
Error: Must introduce 1 or 0
--> fas
df
Error: Must introduce 1 or 0
--> asd
fas
谢谢。
【问题讨论】:
-
您可能想要区分
0或1的成功输入和while (getline(cin,instruction))也失败。