只识别的int输入为1

-1 c++ do-while

无论我输入什么,此函数中唯一识别的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();
    }
}
Run Code Online (Sandbox Code Playgroud)

Pau*_*l R 11

这一行:

while (input != (0 || 1 || 2 || 3 || 4 || 5 || 6))
Run Code Online (Sandbox Code Playgroud)

不会做你认为它做的事情.你不能把这样的测试结合起来.你写的内容基本上等同于while (input != true),并且因为true等于1,你可以看到唯一可行的选项是input == 1.

您需要将其更改为例如

while (input < 0 || input > 6)
Run Code Online (Sandbox Code Playgroud)