验证用户输入为整数:用户必须输入两次

时间:2019-05-20 05:48:08

标签: c++

我正在提示用户输入一个整数值。如果该值不正确,则程序将运行。但是,当用户输入整数输入时,用户需要输入两次。

我看了其他有关如何使用while循环捕获错误输入的教程,该部分对我有用。但是,整数值需要输入两次才能使程序运行。

#include <iostream>
using namespace std;

int main() {
    cout << "*************************************************" << endl;
    cout << "******************|DVD Library|******************" << endl;
    cout << "*************************************************" << endl;
    cout << "1.\tAdd DVD" << endl;
    cout << "2.\tDelete DVD" << endl;
    cout << "3.\tSearch DVD" << endl;
    cout << "4.\tList All DVDs in the Library" << endl;
    cout << "5.\tAdd DVD to Favorites List" << endl;
    cout << "6.\tDelete DVD from Favorites List" << endl;
    cout << "7.\tSearch DVD in Favorites List" << endl;
    cout << "8.\tList All DVDs in Favorites List" << endl;
    cout << "9.\tQuit" << endl;
    cout << "*************************************************" << endl;

    int input;
    cin >> input;
    while (!(cin >> input)) {
        cin.clear();
        while (cin.get() != '\n')
            continue;
        cout << "Please enter an integer --> " << flush;
    }
    if (input < 1 || input > 9) {
        cout << "Invalid input! Please try again!" << endl;
    }
    return 0;
}

4 个答案:

答案 0 :(得分:4)

您要求输入两次:

cin >> input;
while(!(cin >> input )){

删除第一行可能会使其按预期工作。

答案 1 :(得分:4)

“用户必须输入两次输入”查看您的代码

int input;
cin >> input;
while(!(cin >> input )){

您要求用户输入几次?

您会为此而生

int input;
while(!(cin >> input )){

您的错误恢复代码看起来合理,但是尚未测试。

答案 2 :(得分:1)

int input;
while (cout << "Your choice: ",
       !(cin >> input) || input < 1 || 9 < input)
{
    cin.clear();
    while (cin.get() != '\n');
    cerr << "Invalid input! Please try again!\n";
}

答案 3 :(得分:0)

谢谢大家! “ cin >>输入;”行是不必要的。最初,我把它留在那里,因为如果用户输入了数字输入(例如双精度),它将实际上告诉用户错误消息。因此,如果用户输入的内容类似3.3,则程序将显示我指定的错误消息(“请输入整数”行)。但是,这种情况下的程序(当有双精度时)会要求用户两次提示输入整数,然后继续执行该程序。当我删除该不必要的行时,程序会接受双精度输入,但是它的作用是将小数点前的数字值用作整数。因此,我测试时将值1.2记录为1。我不确定为什么会发生这种现象,但是程序可以正常工作。也许是人为错误造成的?

#include <iostream>
using namespace std;

int main() {
    cout << "*************************************************" << endl;
    cout << "******************|DVD Library|******************" << endl;
    cout << "*************************************************" << endl;
    cout << "1.\tAdd DVD" << endl;
    cout << "2.\tDelete DVD" << endl;
    cout << "3.\tSearch DVD" << endl;
    cout << "4.\tList All DVDs in the Library" << endl;
    cout << "5.\tAdd DVD to Favorites List" << endl;
    cout << "6.\tDelete DVD from Favorites List" << endl;
    cout << "7.\tSearch DVD in Favorites List" << endl;
    cout << "8.\tList All DVDs in Favorites List" << endl;
    cout << "9.\tQuit" << endl;
    cout << "*************************************************" << endl;

    int input;
    while (!(cin >> input)) {
        cin.clear();
        while (cin.get() != '\n')
            continue;
        cout << "Please enter an integer --> " << flush;
    }
    if (input < 1 || input > 9) {
    cout << "Invalid input! Please try again!" << endl;
}
return 0;

}