带有 while 语句范围捕获的 if 语句中的 C++ 错误

C++ bug in a if statement with a while statement range catch

大部分情况下我都可以使用此代码。

我遇到的唯一问题是,当我在输入兼容答案后输入不兼容答案时,程序第一次终止而不是重置。

代码采用整数 1..4 作为答案,或采用 -1 作为终止序列的标志,然后 cout 计算结果。

如果没有输入这五个答案中的一个,则应该再次要求输入兼容。

如果输入一次超出范围的输入,在代码的开头,它会正确捕获并运行。

然而,在输入一次正确答案后,如果它收到一个超出范围或不正确的答案-1,序列将停止而不是要求正确输入。

#include <iostream>

using namespace std;

int main()
{
    int coffee = 0;
    int tea = 0;
    int coke = 0;
    int orangeJuice = 0;
    int person = 1;
    int choice;

    cout << "Please input the favorite beverage of person #" << person << ": " 
         << endl 
         << "Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program" 
         << endl;
    cin >> choice;

    while (choice < 1 || choice > 4) {
        cout << "Invalid choice" << endl;
        cout << "Please input the favorite beverage of person #" << person << ": " << endl
             << "Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program" << endl;
        cin >> choice;

    }

    while (choice != -1 && choice >= 1 && choice <= 4) {

        if (choice == 1) {
            person++;
            coffee++;
            cout << "Please input the favorite beverage of person #" << person << 
                ": " << endl
                 << "Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program" << endl;
            cin >> choice;
        }
        else if (choice == 2) {
            person++;
            tea++;
            cout << "Please input the favorite beverage of person #" << person << 
                ": " << endl
                 << "Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program" << endl;
            cin >> choice;
        }
        else if (choice == 3) {
            person++;
            coke++;
            cout << "Please input the favorite beverage of person #" << person << 
                ": " << endl
                 << "Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program" << endl;
            cin >> choice;
        }
        else if (choice == 4) {
            person++;
            orangeJuice++;
            cout << "Please input the favorite beverage of person #" << person << 
                ": " << endl
                 << "Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program" << endl;
            cin >> choice;
        }

    }
    cout << "The total number of people surveyed is " << person << ".  The results are as followed." << endl;
    cout << "Beverage Number of Votes" << endl;
    cout << "**********************************" << endl;
    cout << "Coffee " << coffee << endl;
    cout << "Tea " << tea << endl;
    cout << "Coke " << coke << endl;
    cout << "Orange Juice " << orangeJuice << endl;

    return 0;
}

您没有进行任何错误处理来确保 cin >> choice 成功。如果用户输入的内容不能转换为 int(比如字母而不是数字),operator>> 会失败并在流中设置错误标志,choice 要么是不确定的,要么是 0(取决于在实现上),并且错误的输入保留在输入缓冲区中。在清除错误状态和输入缓冲区之前,operator>> 将一直失败。

此外,您的第一个 while 循环不允许用户输入 -1 作为答案。它满足保持循环提示用户输入的 choice < 1 条件。

此外,您在处理输入和输出的方式上有很多冗余。

尝试更像这样的东西:

#include <iostream>
#include <limits>

using namespace std;

int main()
{
    int coffee = 0;
    int tea = 0;
    int coke = 0;
    int orangeJuice = 0;
    int person = 0;
    int choice;

    do
    {
        cout << "Please input the favorite beverage of person #" << person+1 << ": " << endl;

        do
        {
            cout << "Choose 1, 2, 3, or 4 from the above menu, or -1 to exit the program" << endl;

            if (!(cin >> choice))
            {
                cout << "Invalid input" << endl;
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cin.clear();
                continue;
            }

            if (((choice >= 1) && (choice <= 4)) || (choice == -1))
                break;

            cout << "Invalid choice" << endl;
        }
        while (true);

        if (choice == -1)
            break;

        switch (choice)
        {
            case 1:
                ++coffee;
                break;

            case 2:
                ++tea;
                break;

            case 3:
                ++coke;
                break;

            case 4:
                ++orangeJuice;
                break;
        }

        ++person;
    }
    while (true);

    cout << "The total number of people surveyed is " << person << ".  The results are as followed." << endl;
    cout << "Beverage Number of Votes" << endl;
    cout << "**********************************" << endl;
    cout << "Coffee " << coffee << endl;
    cout << "Tea " << tea << endl;
    cout << "Coke " << coke << endl;
    cout << "Orange Juice " << orangeJuice << endl;

    return 0;
}