cin验证功能有时不会自动运行

时间:2017-04-11 13:03:05

标签: c++ cin

我在编写的程序中有两个cin验证函数 - 一个用于验证int,另一个用于验证double,同时确保用户无法输入char值。我遇到的问题是,有时函数会在要求用户输入值后立即开始验证,如下例所示:

cout << endl << "Enter transaction ID to edit : ";
toEdit = validateIntInput(toEdit, 1, MAX_TRANS);

或者这种情况:

cout << "How much are you adding? : " << char(156);
tVal = validateDoubleInput(tVal, 0.01, 999999998);

但是,在其他情况下,程序不会告诉用户他们的输入无效,只需创建一个新行,就像这样:

cout << "What day of the month is the bill normally paid? (1 - 31) (You can change this later) : ";
paymentDay = validateIntInput(paymentDay, 1, 31);

或者这种情况:

cout << "Annual interest rate (%) : ";
annualInterestRate = validateDoubleInput(annualInterestRate, 0.01, 100);

validateIntInput的代码是:

int validateIntInput(int paramToCheck, int minValue, int maxValue)
{
    paramToCheck = 999999999;
    string line;
    while (getline(cin, line))
    {
        stringstream linestream(line);
        linestream >> paramToCheck;
        // if the first if is not included, the program will assume invalid input has been entered as soon as the user is asked for input
        if (paramToCheck == 999999999)
        {
            cout << "";
            paramToCheck = 0;
        }
        // if the input contains a string or is not within bounds, throw an error
        else if (!linestream.eof() || paramToCheck < minValue || paramToCheck > maxValue)
        {
            cout << red << "Invalid input. Try again : " << white;
        }
        // if the input is valid, stop the loop and accept the input
        else
        {
            break;
        }
    }
    return paramToCheck;
}

validateDoubleInput的代码是:

double validateDoubleInput(double paramToCheck, double minValue, double maxValue)
{
    paramToCheck = 999999999;
    string line;
    while (getline(cin, line))
    {
        stringstream linestream(line);
        linestream >> paramToCheck;
        // if the first if is not included, the program will assume invalid input has been entered as soon as the user is asked for input
        if (paramToCheck == 999999999)
        {
            cout << "";
            paramToCheck = 0;
        }
        // if the input contains a string or is not within bounds, throw an error
        else if (!linestream.eof() || paramToCheck < minValue || paramToCheck > maxValue)
        {
            cout << red << "Invalid input. Try again : " << white;
        }
        // if the input is valid, stop the loop and accept the input
        else
        {
            break;
        }
    }
    return paramToCheck;
}

注意:函数为参数赋值999999999并在启动时检查它的唯一原因是程序有时甚至在用户输入任何内容之前就抛出了异常。

我真的不知道这里会出现什么问题 - 有人能帮我解决问题的根源吗?

提前感谢任何有能力的人!

1 个答案:

答案 0 :(得分:0)

正如我在评论部分所述,我没有得到跳过输入验证的情况。我得到的最接近的是当我输入两次char值时,其最小可接受值为0.在这种情况下,它只返回0.

在没有告诉您价值无效的情况下,因为在您的支票中您正在使用

    if (paramToCheck == 999999999)
    {
        cout << "";  // Doesn't print out an error like you want it to
        paramToCheck = 0;
    }

我会重新安排你的功能来代替:

if (!linestream.eof() || paramToCheck < minValue || 
        paramToCheck > maxValue || paramToCheck == 999999999)
    {
        cout << red << "Invalid input. Try again : " << white;
    }