如何进入如果我输入一个字母输入我的程序陷入了它的while循环? [英] How come if I enter a letter input my program gets stuck in its while loop?

查看:155
本文介绍了如何进入如果我输入一个字母输入我的程序陷入了它的while循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果我不够清楚或犯错误,这是我第一次发布。

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

我的代码运行没有错误,但编译,但第一个while循环(a)为 cin>键入一个字母( int main > 1 2 3

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.

为什么是这样,我该怎么做才能使它正常运行?

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 << "\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;
}


推荐答案

我是一个新手编程一般来说,但是玩你的代码和查找东西让我找到一些解决方案。
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(#,'\n') (# being a very, very large number) to have it avoid the line and skip right through it.

找到解决方案中的what-we-call-cin-clear-and-cin-ignore-after-reading-input>解释了两个cin命令的使用。

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

这篇关于如何进入如果我输入一个字母输入我的程序陷入了它的while循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆