【问题标题】:How come if I enter a letter input my program gets stuck in its while loop?如果我输入一个字母,我的程序怎么会卡在它的 while 循环中?
【发布时间】:2014-04-03 02:17:07
【问题描述】:

对不起,如果我不够清楚或有任何错误,这是我第一次发帖。

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

但是,当我输入 "4" 或任何其他随机数字字符串时,它运行良好,并按应有的方式转到我的错误消息。

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

我的代码:

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

void calculator();
void unavailableitem();

int main()
{
    string select;
    while (true)
    {
        cout << "\t[Main Menu]\n";
        cout << " 1. Calculator\n";
        cout << " 2. [unavailable]\n";
        cout << " 3. [unavailable]\n";
        cout << "\n 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 << "\nInvalid response.\n";
    }
}

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

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

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

    cout << "List of operations:\n";
    cout << " 1. Addition\n";
    cout << " 2. Subtraction\n";
    cout << " 3. Multiplication\n";
    cout << " 4. Division\n";
    cout << "Enter the number on the left to pick an operation: ";
    cin >> choice;
    cout << "\nEnter number 1: ";
    cin >> num1;
    cout << "\nEnter 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 << "\t[Main Menu]\n";
        cout << " 1. Calculator\n";
        cout << " 2. [unavailable]\n";
        cout << " 3. [unavailable]\n";
        cout << "\n Enter the number of your selection: ";
        cin >> select;

        if(!(cin >> select))
        {
        cout << "Input must be an integer.\n";
        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 << "\n \t [ITEM UNAVAILABLE]\n";
    while (true)
    {
        cout << "\nEnter 'menu' to return to main menu: ";
        cin >> react;

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

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

    cout << "List of operations:\n";
    cout << " 1. Addition\n";
    cout << " 2. Subtraction\n";
    cout << " 3. Multiplication\n";
    cout << " 4. Division\n";
    cout << "Enter the number on the left to pick an operation: ";
    cin >> choice;
    cout << "\nEnter number 1: ";
    cin >> num1;
    cout << "\nEnter 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;
}

【问题讨论】:

  • 感谢您的回复。我读了这个,但我不明白。我是 C++ 新手。
  • 现在肯定有数百万个类似的问题。请花一三天时间浏览 Stack Overflow。
  • 你不应该在你的程序中调用main。由于您从main 调用unavailableitem,只需键入return; 即可退出该函数。此外,虽然您的程序“短”且可编译,但需要输入的程序通常更难调试,特别是如果它需要一长串输入。尝试最小化您的程序,使其几乎不需要输入。最后请去掉letter标签。
  • 不要在你的程序中调用 main 这是不允许的。看Is it legal to recurse into main() in C++?

标签: c++ loops while-loop


【解决方案1】:

我是一般编程的新手,但是玩你的代码和查找东西让我找到了某种解决方案。 cin.clear只是清除了输入的错误日志,相信还是保留了字母的值。

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

找到了解决方案in another question,它解释了两个 cin 命令的使用。

【讨论】:

  • 天哪。有用!现在直接进入错误。太感谢了!我不明白这是如何工作的,但我会阅读你链接的那个问题。我很高兴,我被困在这上面了。再次感谢。是时候阅读和学习一些东西了。我很惊讶你认为自己是一个新手,但能想出类似的东西,因为它对我来说似乎很神秘。是时候开始学习了。
【解决方案2】:

Ad Ed Heal 提到,这里的问题是 cin 的故障位。当您执行cin &gt;&gt; choice 并且用户键入“a”时,转换为int 将失败。这将设置cin 的故障位,使所有未来的读取都失败,直到故障位被清除。因此,下次您到达cin &gt;&gt; choice 时,用户甚至都无法输入任何内容。

您可以使用cin.clear() 恢复正常工作状态。

要更稳健地执行此操作,您可以执行类似的操作

while(true)
{
    cout >> "Enter choice [1-4]: ";
    if(!(cin >> choice))
    {
        cout << "Input must be an integer.\n";
        cin.clear();
        continue;
    }
    do_stuff_with_choice();
 }    

【讨论】:

  • 好的,非常感谢,既然你清理了它,这对我来说开始更有意义了。我添加了您的代码并创建了一个do_stuff_with_choice(); 函数,但在第一次将“a”输入我的程序后,我仍然遇到相同的循环错误。我在旧代码的正下方发布了我的新代码。
  • 您有cin &gt;&gt; select;,后跟if(!(cin &gt;&gt; select))。摆脱第一个cin &gt;&gt; select;
  • 我实际上指的是calculator 中的循环。 main 中的那个使用了string,而不是int,因此不会出现任何转换失败。
猜你喜欢
  • 2022-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多