也就是说,我有这样的代码:C++中的整数和while循环...怎么样?
int main {
cin >> x;
while (x != 1 && x != 2 && x != 3) {
cout << "Invalid input, try again!" << endl;
cin >> x;
}
cout << "Hello, World";
}
它应该返回“你好,世界”当整数变量“x”为1,2或3
但是,当我键入一个非整数值(即“你好”),它为什么会进入循环?
你必须在'std :: cin >>'后检查错误(并重置它)。 – Jarod42 2014-09-24 15:00:49
我该怎么做? – 2014-09-24 15:01:49
看看[error-handling-in-c](http://stackoverflow.com/questions/3824921/error-handling-in-c) – Jarod42 2014-09-24 15:03:14