【问题标题】:Integer validation for input输入的整数验证
【发布时间】:2013-05-31 19:32:21
【问题描述】:

我试图提示用户输入并进行验证。例如,我的程序必须接受 3 个用户输入。一旦它达到非整数,它将打印错误消息并再次提示输入。这是我的程序在运行时的样子:

输入数字:一个

输入错误

输入数字:1

输入数字:b

输入错误

输入数字:2

输入数字:3

输入的数字是 1,2,3

这是我的代码:

double read_input()
{
    double input;
    bool valid = true;
    cout << "Enter number: " ;
    while(valid){
        cin >> input;
        if(cin.fail())
        {
            valid = false;
        }
    }
    return input;
}

我的主要方法:

int main()
{
double x = read_input();
double y = read_input();
double z = read_input();
}

当我的第一个输入是非整数时,程序会自行退出。它不会再次要求提示。我怎么能修好它?或者我应该使用 do while 循环,因为我要求用户输入。

提前致谢。

【问题讨论】:

标签: c++ validation while-loop


【解决方案1】:

当读取失败时,你将valid设置为false,所以while循环中的条件为false,程序返回input(顺便说一下没有初始化)。

您还必须在再次使用之前清空缓冲区,例如:

#include <iostream>
#include <limits>

using namespace std;

double read_input()
{
    double input = -1;
    bool valid= false;
    do
    {
        cout << "Enter a number: " << flush;
        cin >> input;
        if (cin.good())
        {
            //everything went well, we'll get out of the loop and return the value
            valid = true;
        }
        else
        {
            //something went wrong, we reset the buffer's state to good
            cin.clear();
            //and empty it
            cin.ignore(numeric_limits<streamsize>::max(),'\n');
            cout << "Invalid input; please re-enter." << endl;
        }
    } while (!valid);

    return (input);
}

【讨论】:

  • 当我在 else 语句中添加一个 cout 并输入非整数时,错误消息本身就一直在循环。
  • 我无法测试代码,但 Ideone 有相同的输出,让我看看原因。
  • @Carol 现在可以了,我猜是clearignore 之间的顺序:ideone.com/fl4IMK
  • 好的,非常感谢。但是为什么要把输入初始化为-1,flush有什么用呢?
  • @Carol 在 C 和 C++ 中,我总是初始化我的值(Java 抱怨它,所以我现在总是这样做),你不能在没有读取正确值的情况下退出函数,所以没有必要。 Flush 是为了确保标准输入缓冲区正确显示,我是这样认为的,如果你愿意,你可以删除它。 (我认为 endl 是 '\n' + flush)。
【解决方案2】:

你的问题确实让我自己陷入了其他问题,比如在 fail() 上清除 cin --

double read_input()
{
double input;
int count = 0; 
bool valid = true;
while(count != 3) {
    cout << "Enter number: " ;
    //cin.ignore(); 
    cin >> input; 
    if(cin.fail())
    {
        cout << "Wrong Input" <<endl;
        cin.clear(); 
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
    }
    else 
            count++;
}
return input;
}

【讨论】:

    【解决方案3】:

    问题在于while条件

    bool valid = true;
    while(valid){
    

    你循环直到你得到一个无效的输入,这绝对不是你想要的!循环条件应该是这样的

    bool valid = false;
    while(! valid){ // repeat as long as the input is not valid
    

    这是您的 read_double 的修改版本

    double read_input()
    {
        double input;
        bool valid = false;
        while(! valid){ // repeat as long as the input is not valid
            cout << "Enter number: " ;
            cin >> input;
            if(cin.fail())
            {
                cout << "Wrong input" << endl;
    
                // clear error flags
                cin.clear(); 
                // Wrong input remains on the stream, so you need to get rid of it
                cin.ignore(INT_MAX, '\n');
            }
            else 
            {
                valid = true;
            }
        }
        return input;
    }
    

    例如,在您的主要要求中,您需要尽可能多地要求加倍,例如

    int main()
    {
        double d1 = read_input();
        double d2 = read_input();
        double d3 = read_input();
    
        cout << "Numbers entered are: " << d1 << ", " << d2 << ", " << d3 << endl;
    
        return 0;
    }
    

    您可能还希望有一个循环,您可以在其中调用 read_double() 并将返回的值保存在一个数组中。

    【讨论】:

    • 好的,非常感谢。我在 else 语句中使用了 cin.clear() 和 getline,它现在可以工作了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 2019-06-02
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 2014-03-06
    • 2021-10-20
    相关资源
    最近更新 更多