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

时间:2018-06-22 00:07:19

标签: c++ if-statement sentinel

在大多数情况下,我可以使用此代码。

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

代码使用整数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;
}

1 个答案:

答案 0 :(得分: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;
}