C++无限循环

标签 c++ while-loop

我正在尝试编写一个循环,该循环会一直重复,直到用户输入正确的选项之一(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/

相关文章:

c - 在C中,使用while循环来计算用户输入的字符数

python - 关闭 python MySQL 脚本

css - 带有 sass 的大字母轮廓描边

c++ - 如何从 PathCompactPath 的 Wnd 句柄获取 DC 句柄?

c++ - cocos2d-x 3.2 中从左向右移动进度条

java - 如何在没有break语句的情况下键入BinarySearch方法while循环?

c# - 如何在 C# 中使用 3 个数组分配变量

c++ - C/C++ 赋值给 std::queue<char*>

c++ - 超过时间限制 - 简单程序 - 整除测试

c++ - 模板友好语法