我正在尝试编写一个循环,该循环会一直重复,直到用户输入正确的选项之一(1 或 0)。出于某种原因,当我将循环编写如下时,它会创建一个无限循环。
我打算让循环仅在控制不为 0 或不为 1 时执行,但由于某种原因它将始终执行并成为无限循环。
cout<<"Please enter 1 for another customer or 0 to quit : ";
cin>>control;
<b>while ((control != 0 )|| (control != 1))</b>
{
cout<<"Invalid Entry! Please enter a 1 to enter another customer or 0 to quit: ";
cin>>control;
}
我将其更改为 while control 小于 0 或大于 1,这有效,但我仍然对为什么另一个循环不起作用感到困惑。
最佳答案
您必须使用 && 运算符。
while ((control != 0 ) && (control != 1))
关于C++无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19897995/