Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
输入验证需要输入两次 我对编程很陌生,而且我对下面C++程序的输入验证有一些麻烦。p>_C++_Validation_Debugging_Input_While Loop - Fatal编程技术网

输入验证需要输入两次 我对编程很陌生,而且我对下面C++程序的输入验证有一些麻烦。p>

输入验证需要输入两次 我对编程很陌生,而且我对下面C++程序的输入验证有一些麻烦。p>,c++,validation,debugging,input,while-loop,C++,Validation,Debugging,Input,While Loop,我的目标是防止用户在给定域[100999]中输入除整数以外的任何内容 输入第一个数字后,程序似乎暂停。用户必须输入第二个值,然后代码才能使用cin>>number.fail,再次调用cin>>。只要取出第一个cin,你就会没事。谢谢你,Thornkey。这是最快的答复。你的解决方案奏效了! #include <iostream> #include <iomanip> #include <math.h> #include <

我的目标是防止用户在给定域[100999]中输入除整数以外的任何内容


输入第一个数字后,程序似乎暂停。用户必须输入第二个值,然后代码才能使用cin>>number.fail,再次调用cin>>。只要取出第一个cin,你就会没事。

谢谢你,Thornkey。这是最快的答复。你的解决方案奏效了!
    #include <iostream>
    #include <iomanip>
    #include <math.h>
    #include <limits>
    using namespace std;

    int main() {

        int number = 0;

        cout << "Please enter a whole number from 100 to 999:\n\n";
        cin >> number;


        //Check to see if entered value is a character 
        //or integer is within the given range

        while ((cin >> number).fail() || cin.peek() != '\n' || number > 999 || number < 100)
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');

            cout << "Please enter an integer from 100 to 999:\n";
        }

        cout << "you entered " << number << endl;

        char q;
        cin >> q;

        return 0;
    }