如果我输入一个字母,我的程序怎么会卡在它的 while 循环中?

2021-12-26 00:00:00 loops while-loop c++

对不起,如果我说得不够清楚或犯了任何错误,这是我第一次发帖.

Sorry if I fail to be clear enough or make any mistakes, this is my first time posting.

我的代码在编译时运行没有错误,但第一个 while 循环(在 int main 中)在用户输入字母(如 "a")时卡住循环cin>>select; 而不是必需的 123.

My code runs without errors when complied but the first while loop (in int main) gets stuck looping whenever a user types a letter (like "a") for cin >> select; instead of the required 1, 2, or 3.

但是,当我输入 "4" 或任何其他随机数字字符串时,它运行良好并像它应该的那样转到我的错误消息.

However, when I input "4" or any other random string of numbers, it runs fine and goes to my error message like it should.

这是为什么,我该怎么做才能让它正常运行?(运行错误消息以响应输入的字母,就好像它们是数字一样).

Why is this and what can I do to make it run normally? (run the error message in response to letters entered as if they were numbers).

我的代码:

#include <iostream>
#include <string>
using namespace std;

void calculator();
void unavailableitem();

int main()
{
    string select;
    while (true)
    {
        cout << "	[Main Menu]
";
        cout << " 1. Calculator
";
        cout << " 2. [unavailable]
";
        cout << " 3. [unavailable]
";
        cout << "
 Enter the number of your selection: ";
        cin >> select;

        if (select == "1")
        {
            cout << endl;
            calculator();
            break;
        }
        else if (select == "2")
        {
            unavailableitem();
            break;
        }
        else if (select == "3")
        {
            unavailableitem();
            break;
        }
        else
            cout << "
Invalid response.
";
    }
}

void unavailableitem()
{
    string react;
    cout << "
 	 [ITEM UNAVAILABLE]
";
    while (true)
    {
        cout << "
Enter 'menu' to return to main menu: ";
        cin >> react;

        if (react == "menu")
        {
            cout << endl;
            main();
            break;
        }
        else
            cout << "
Invalid response.
";
    }
}

void calculator()
{
    int choice;
    double num1;
    double num2;
    double answer;
    string choicesymbol;

    cout << "List of operations:
";
    cout << " 1. Addition
";
    cout << " 2. Subtraction
";
    cout << " 3. Multiplication
";
    cout << " 4. Division
";
    cout << "Enter the number on the left to pick an operation: ";
    cin >> choice;
    cout << "
Enter number 1: ";
    cin >> num1;
    cout << "
Enter number 2: ";
    cin >> num2;

    if (choice == 1)
    {
        answer = num1 + num2;
        choicesymbol = " + ";
    }

    if (choice == 2)
    {
        answer = num1 - num2;
        choicesymbol = " - ";
    }

    if (choice == 3)
    {
        answer = num1 * num2;
        choicesymbol = " * ";
    }

    if (choice == 4)
    {
        answer = num1 / num2;
        choicesymbol = " / ";
    }

    cout << endl;
    cout << num1 << choicesymbol << num2 << " = " << answer;
}

新代码:

#include <iostream>
#include <string>
using namespace std;

void calculator();
void unavailableitem();


int main()
{
    int select;
    while (true)
    {
        cout << "	[Main Menu]
";
        cout << " 1. Calculator
";
        cout << " 2. [unavailable]
";
        cout << " 3. [unavailable]
";
        cout << "
 Enter the number of your selection: ";
        cin >> select;

        if(!(cin >> select))
        {
        cout << "Input must be an integer.
";
        cin.clear();
        continue;
        }
        else if (select == 1)
        {
            cout << endl;
            calculator();
            break;
        }
        else if (select == 2)
        {
            unavailableitem();
            break;
        }
        else if (select == 3)
        {
            unavailableitem();
            break;
        }
    }
}



void unavailableitem()
{
    string react;
    cout << "
 	 [ITEM UNAVAILABLE]
";
    while (true)
    {
        cout << "
Enter 'menu' to return to main menu: ";
        cin >> react;

        if (react == "menu")
        {
            cout << endl;
            return;
            break;
        }
        else
            cout << "
Invalid response.
";
    }
}

void calculator()
{
    int choice;
    double num1;
    double num2;
    double answer;
    string choicesymbol;

    cout << "List of operations:
";
    cout << " 1. Addition
";
    cout << " 2. Subtraction
";
    cout << " 3. Multiplication
";
    cout << " 4. Division
";
    cout << "Enter the number on the left to pick an operation: ";
    cin >> choice;
    cout << "
Enter number 1: ";
    cin >> num1;
    cout << "
Enter number 2: ";
    cin >> num2;

    if (choice == 1)
    {
        answer = num1 + num2;
        choicesymbol = " + ";
    }

    if (choice == 2)
    {
        answer = num1 - num2;
        choicesymbol = " - ";
    }

    if (choice == 3)
    {
        answer = num1 * num2;
        choicesymbol = " * ";
    }

    if (choice == 4)
    {
        answer = num1 / num2;
        choicesymbol = " / ";
    }

    cout << endl;
    cout << num1 << choicesymbol << num2 << " = " << answer;
}

推荐答案

总的来说,我是编程的新手,但是使用您的代码和查找内容让我找到了某种解决方案.cin.clear 只清除输入的错误日志,相信还是保留了字母的值.

I am a newbie to programming in general, but playing with your code and looking up stuff made me find some sort of solution. The cin.clear only clears the error log of the input, and I believe that it still retains the value of the letter.

你应该在后面添加一个 cin.ignore(#,' ')(# 是一个非常非常大的数字)让它避开该行并直接跳过它.

What you should add right after is a cin.ignore(#,' ') (# being a very, very large number) to have it avoid the line and skip right through it.

在另一个中找到了解决方案 问题解释了两个cin命令的使用.

Found the solution in another question that explains the use of both cin commands.

相关文章