C++ 仅识别的int输入为1

C++ 仅识别的int输入为1,c++,do-while,C++,Do While,不管我输入什么,这个函数中唯一能识别的int是1。 如果选择了任何其他选项,则会重复do while循环。 代码在没有任何OR运算符的情况下运行良好,例如“while(input!=0)” void菜单() { int输入=-1; 做 { cout这条线: while (input != (0 || 1 || 2 || 3 || 4 || 5 || 6)) 不做你认为它做的事情。你不能像这样组合测试。你所写的基本上等同于,而(input!=true),并且由于true等于1,你可以看到唯一有效

不管我输入什么,这个函数中唯一能识别的int是1。 如果选择了任何其他选项,则会重复do while循环。 代码在没有任何OR运算符的情况下运行良好,例如“while(input!=0)”

void菜单()
{
int输入=-1;
做
{
cout这条线:

while (input != (0 || 1 || 2 || 3 || 4 || 5 || 6))
不做你认为它做的事情。你不能像这样组合测试。你所写的基本上等同于
,而(input!=true)
,并且由于
true
等于1,你可以看到唯一有效的选项是
input==1

你需要把它改成

while (input < 0 || input > 6)
while(输入<0 | |输入>6)

您正在将
输入与
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(输入<0 | |输入>6)
或者更迂回一些,

while (!(input >= 0 && input <= 6))

while(!(input>=0&&input正如其他人所指出的,使用| |(逻辑或)将使您的条件等价于
(input!=1)

我建议在此使用switch/case语句和附加条件:

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 ) ;
}
void菜单()
{
int输入=-1;
bool-inputIsValid=false;
做
{

我能更喜欢关于为什么测试会做它所做的事情的额外细节吗。
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 ) ;
}