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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带有while语句范围catch的if语句中的C ++错误相关的知识,希望对你有一定的参考价值。

在大多数情况下,我有这个代码工作。

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

代码采用整数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(), '
');
                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;
}

以上是关于带有while语句范围catch的if语句中的C ++错误的主要内容,如果未能解决你的问题,请参考以下文章

c语言中while(num)或if(num)就执行语句,num表示啥?

[dart学习]第六篇:流程控制语句

带有嵌套 if/else 语句的 While 循环

带有嵌套 if 语句的 while 循环的 Big-O 运行时

javascript基础学习

循环语句中while(1)中的1是啥意思