0
只是试图检查输入是否为此问题的布尔变量(1或0)。cin.fail while循环输入重新输入
但是,无论何时触发while循环(即不通过输入1或0),它都会通过并说条件为真。
我希望用户能够在错误输入后重新输入他们的输入。我会如何去做这件事?
我的代码:
int main() {
bool a,b,c;
cout << "This program will determine the value of the condition !(a||b) && c " << endl;
cout << "Please enter boolean values for a, b, and c. (0=F, 1=T) ";
cin >> a;
while (cin.fail())
{
cin.ignore(1000,'|n');
cin.clear();
cout << "Error: Enter only 0 or 1" << endl;
cout << "Enter in a new value for a" << endl;
}
cin >> b;
cin >> c;
cout << "The condition !(xx||xx)&&xx ";
if(!(a||b) && c)
{
cout << "is true";
}
else
{
cout << "is false";
}
return 0;
}
谢谢。但是,如果我输入一个字母,为什么它会在我的控制台中无限重复两个输出?有没有简单的方法来解决这个问题? – kpnd
@kpnd:在你的问题中有错字,这个答案的换行字符字面值 - 它应该是''\ n''不是''| n'' ......更好的修复并再试一次。 –
@TonyD感谢您的支票。但是,输入非数字时无限循环的问题仍然存在。有任何想法吗? – kpnd