【问题标题】:Exception Handling: stuck in infinite loop if a char is entered异常处理:如果输入字符,则陷入无限循环
【发布时间】:2021-12-29 01:14:50
【问题描述】:

我的异常处理有问题。它似乎适用于负输入,但每当我尝试输入一个字符时,它就会在第一个 try/catch 块中陷入无限循环。

我尝试使用clear()ignore() 换成cin,但还是不行。我尝试了不同的if 语句作为输入,例如(if ((ft >100)) || (in >100)),抛出异常就好了。

我错过了什么吗?

#include <iostream>
using namespace std;

class convert
{
private:
    int feet;
    int inch;
public:
    convert() = default;
    double calculation(int ft, int in)
    {
        double cm = 0;

        cm = ((ft * 30.48) + (in * 2.54));

        return cm;
    }
};

class negativeNumber
{};

class invalidInput
{};

int main()
{
    int ft;
    int in;
    double cm = 0;
    convert h;
    while (true)
    {
        try
        {
            cout << "Please enter your height in (ft, in) format" << endl;
            cin >> ft;
            cin >> in;

            if ((ft < 0) || (in < 0))
            {
                throw negativeNumber();
            }
            if (cin.fail())
            {
                throw invalidInput();
            }
            cm = h.calculation(ft, in);
            cout << "Your height in cm is: " << cm << endl;
            break;
        }
        catch (negativeNumber)
        {
            cout << "You entered negative numbers. Try again" << endl;
            system("pause");
        }
        catch (invalidInput)
        {
            cout << "You entered a character. Try again" << endl;
            system("pause");
        }
    }
    return 0;
}

【问题讨论】:

  • 这并没有解决问题,但在cm = ((ft * 30.48) + (in * 2.54)); 中,您不需要任何这些括号。

标签: c++ exception


【解决方案1】:

当输入读取失败时,您绝对需要调用cin.clear()cin.ignore()。您需要清除cin 的错误状态才能继续读取输入,并且您需要从cin 的输入缓冲区中删除失败的数据,这样您就不会一遍又一遍地读取相同的数据。

此外,您应该检查输入失败之前检查否定。 ftin 如果读取失败,则它们将没有有效值。

此外,您应该始终通过 (const) 引用而不是值来捕获异常。

试试这个:

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

class convert
{
public:
    static double calculation(int ft, int in)
    {
        double cm = 0;
        cm = ((ft * 30.48) + (in * 2.54));
        return cm;
    }
};

class negativeNumber
{};

class invalidInput
{};

int main()
{
    int ft;
    int in;
    double cm;

    while (true)
    {
        try
        {
            cout << "Please enter your height in (ft, in) format" << endl;
            if (!(cin >> ft >> in))
            {
                throw invalidInput();
            }

            if ((ft < 0) || (in < 0))
            {
                throw negativeNumber();
            }

            cm = convert::calculation(ft, in);
            cout << "Your height in cm is: " << cm << endl;
            break;
        }
        catch (const negativeNumber &)
        {
            cout << "You entered a negative number. Try again" << endl;
            system("pause");
        }
        catch (const invalidInput &)
        {
            cout << "You entered bad input. Try again" << endl;
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            system("pause");
        }
    }
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 2012-09-28
    • 1970-01-01
    相关资源
    最近更新 更多