C++ 输入的整数验证

C++ 输入的整数验证,c++,validation,while-loop,C++,Validation,While Loop,我试图提示用户输入并进行验证。例如,我的程序必须接受3个用户输入。一旦点击非整数,它将打印错误消息并再次提示输入。下面是我的程序在运行时的样子: 输入数字:a 输入错误 输入数字:1 输入数字:b 输入错误 输入数字:2 输入数字:3 输入的数字为1,2,3 这是我的代码: double read_input() { double input; bool valid = true; cout << "Enter number: " ; while(va

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

输入数字:a

输入错误

输入数字: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;
}
当我的第一个输入是非整数时,程序只会自动退出。它不再要求提示。我怎么能修好它?或者我应该使用do-while循环,因为我请求用户输入


提前感谢。

当读取失败时,您将
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);
}
#包括
#包括
使用名称空间std;
双读输入()
{
双输入=-1;
bool valid=false;
做
{
不能输入;
if(cin.good())
{
//一切顺利,我们将退出循环并返回值
有效=真;
}
其他的
{
//出了问题,我们将缓冲区的状态重置为良好
cin.clear();
//然后清空它
cin.ignore(数值限制::max(),'\n');

你的问题确实让我陷入了其他问题,比如在失败时清除cin()

双读输入()
{
双输入;
整数计数=0;
bool valid=true;
while(计数!=3){
cout>输入;
if(cin.fail())
{

问题出在while条件下

bool valid = true;
while(valid){
你循环直到你得到一个无效的输入,这绝对不是你想要的!循环条件应该是这样的

bool valid = false;
while(! valid){ // repeat as long as the input is not valid
这是您的
read\u 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;
}
双读输入()
{
双输入;
bool valid=false;
while(!valid){//只要输入无效,就重复该操作
cout>输入;
if(cin.fail())
{

我们可以看更多的代码吗..你用调试器检查过了吗?可能是崩溃了…在主方法中我只写了double x=read_input();double y=read_input();double z=read_input();我做得不对吗?还有,当我在else语句中输入cout,然后输入非整数时,错误消息本身就一直在循环。我无法测试代码,但Ideone有相同的输出,让我检查一下原因。@Carol它现在起作用了,我猜这是
清除
忽略
之间的顺序:好的,非常感谢。但是为什么你要初始化输入I- 1,冲洗有什么用?@凯罗尔和C++中我总是初始化我的值(java抱怨,所以我现在总是这样做),如果没有读取正确的值,则无法退出该函数,因此不需要。Flush是为了确保标准输入缓冲区正确显示,我认为是这样的,如果需要,您可以删除它。(endl是'\n'+Flush,我想)。好的,非常感谢。我在else语句中使用了cin.clear()和getline,现在可以使用了
bool valid = false;
while(! valid){ // repeat as long as the input is not valid
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;
}