仅识别的 int 输入为 1
Only int input recognized is 1
无论我输入什么,此函数中唯一识别的 int 是 1。如果选择了任何其他内容,则 do-while 循环将重复。该代码在没有任何OR运算符的情况下也能正常工作,例如"while (input != 0)"
void menu()
{
int input = -1;
do
{
cout << " ---------------" << endl << " - OU6 -" << endl << " ---------------" << endl;
cout << "1. Read a transaction from the keyboard." << endl;
cout << "2. Print all transactions to console." << endl;
cout << "3. Calculate the total cost." << endl;
cout << "4. Get debt of a single person." << endl;
cout << "5. Get unreliable gold of a single person." << endl;
cout << "6. List all persons and fix." << endl;
cout << "0. Save and quit application." << endl;
cin >> input;
} while (input != (0 || 1 || 2 || 3 || 4 || 5 || 6));
if (input == 0)
{
cout << "0!" << endl;
cin.get();
}
if (input == 1)
{
cout << "1!" << endl;
cin.get();
}
if (input == 2)
{
cout << "2!" << endl;
cin.get();
}
}
这一行:
while (input != (0 || 1 || 2 || 3 || 4 || 5 || 6))
不做你认为它做的事情。您不能像这样组合测试。您编写的内容基本上等同于 while (input != true)
,并且由于true
等于 1,您可以看到唯一有效的选项是 input == 1
.
您需要将其更改为例如
while (input < 0 || input > 6)
您正在将input
与0 || 1 || 2 || 3 || 4 || 5 || 6
进行比较。
||
的结果是一个布尔值,一个真值。
0 || 1 || 2 || 3 || 4 || 5 || 6
相当于
0 != 0 || 1 != 0 || 2 != 0 || 3 != 0 || 4 != 0 || 5 != 0 || 6 != 0
这是(希望很明显)正确的。
而一个bool
可以隐式转换为int
,true
转换为1
,所以这就是你唯一有效输入的原因;你的条件等价于input != 1
。
翻译成逻辑的英语短语"如果输入不是 0 或 1"是"如果输入不是 0 并且输入不是 1"。
这会给你
while (input != 0 && input != 1 && input != 2 && input != 3 && input != 4 && input != 5 && input != 6)
这是一个严重的混乱,写得更明智的是
while (input < 0 || input > 6)
或者,更迂回,
while (!(input >= 0 && input <= 6))
正如其他人指出的那样,使用 ||(逻辑 OR) 将使您的条件等同于 (input != 1)
。
我建议在这里使用开关/案例语句和一个附加条件:
void menu()
{
int input = -1;
bool inputIsValid = false ;
do
{
cout << " ---------------" << endl << " - OU6 -" << endl << " ---------------" << endl;
cout << "1. Read a transaction from the keyboard." << endl;
cout << "2. Print all transactions to console." << endl;
cout << "3. Calculate the total cost." << endl;
cout << "4. Get debt of a single person." << endl;
cout << "5. Get unreliable gold of a single person." << endl;
cout << "6. List all persons and fix." << endl;
cout << "0. Save and quit application." << endl;
cin >> input;
switch ( input )
{
case 0:
cout << "0!" << endl;
cin.get();
inputIsValid = true;
break;
/* other cases here... */
default:
inputIsValid = false ;
break;
}
} while ( false == inputIsValid ) ;
}
相关文章:
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- C++ 中文件输入输出中的长长 v/s int
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- 我想知道为什么"std::unique_ptr<int> foo(新 int)"是合法的,因为"std::<int>unique_ptr"要求输入参数类型应该是"int"?
- 如何在使用 cin 请求 int 时有效地使用户输入万无一失?
- 为什么当我尝试将char输入到int时,文件指针会卡住
- 当我们声明 cin 为 int 并从 cin 中获取输入并在 cout 中打印 cin 时会发生什么?
- 如果我在 C++ 中为 int 提供双精度,输入缓冲区中存储了什么?
- 利用输入文件中的数据(字符串和int)并在循环中利用
- 如何在数组中搜索之前已输入的 int 值
- 当我输入一个字母时,cin 输入(输入是一个 int),而不是打印错误一次,它打印正确一次,然后在循环的其余部分打印 i
- 将输入到向量的<int>优雅解决方案
- 将int输入函数,将int输入开关,然后更改int的值
- 如果用户在 int 中输入字符,如何使用输入
- 如何从输入中定义的"external" int 定义结构中的数组大小
- 当用户输入int变量的字符串时,输出
- 输入int之后的字符的c++输入数组
- C 从文本文件中将数据输入INT矢量
- 福尔。while 循环:我应该怎么做,使输入 (int) 不包含数字 '1' 或"0"?
- 如何在等待用户输入int, float或其他类型时拒绝输入char/string