简体   繁体   English

如何在不退出do while循环的情况下验证输入?

[英]How do I validate input without exiting my do while loop?

I'm working on a program that prompts the user to select from 3 different options by inputing an integer from 1-3 (4 to quit). 我正在开发一个程序,该程序通过输入1-3(退出4)来提示用户从3个不同的选项中进行选择。 I need to write a code that validates that the input is an integer, and reprompts them if it is not an integer. 我需要编写一个代码来验证输入是否为整数,如果不是整数则重新提示它们。 Here's the basic idea of my code (it is too long to post in entirety). 这是我的代码的基本思想(整个发布太久了)。

do 
{
cout << "Menu: Please select one of the following options:" << endl;
    cout << " 1 - Drop a single chip into one slot." << endl;
    cout << " 2 - Drop multiple chips into one slot." << endl;
    cout << " 3 - Drop 5 chips into each slot." << endl;
    cout << " 4 - Quit the program." << endl;
    cout << "Enter your selection now: ";
    cin >> first_input;
}while (first_input!=4)

I then have multiple if statements that execute expressions based on which option the user chooses. 然后,我有多个if语句根据用户选择的选项执行表达式。 I also prompt them to enter other integer values later in the code. 我还将提示他们稍后在代码中输入其他整数值。

How do I send the user back to the menu, if the user inputs fails to input an integer and instead inputs characters? 如果用户输入未能输入整数而是输入字符,如何将用户送回菜单? Constraints: Cannot use continue or break 约束:不能使用continuebreak

Thanks in advance. 提前致谢。

If you want to go back to the start in case of non-integer input, perhaps something like this would work? 如果您想重新开始使用非整数输入,也许像这样的事情行得通吗?

// Insert after "cin >> first_input;"
if (cin.fail()) {
    // Handle non-int value.

    // Clear error flag.
    cin.clear();

    // Empty buffer up to next newline.
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    // Complain & restart.
    cout << "Invalid input.  Please try again." << endl;
    continue;
}

What this does is clear the error flag, purge the buffer, and go back to the start. 这样做是清除错误标志,清除缓冲区,然后重新开始。 cin.ignore() doesn't explicitly need to be passed std::numeric_limits<std::streamsize>::max() as its first parameter; cin.ignore()不需要显式地传递std::numeric_limits<std::streamsize>::max()作为其第一个参数; however, in case of error, I prefer to do so to make sure that the erroneous input is gone. 但是,如果出现错误,我更愿意这样做,以确保错误的输入消失了。 Note that std::numeric_limits is defined in the standard header <limits> , and requires its inclusion. 请注意, std::numeric_limits是在标准头文件<limits>定义的,并且需要将其包括在内。

您正在寻找continue关键字。

do 
{
    cout << "Menu: Please select one of the following options:" << endl;
    cout << " 1 - Drop a single chip into one slot." << endl;
    cout << " 2 - Drop multiple chips into one slot." << endl;
    cout << " 3 - Drop 5 chips into each slot." << endl;
    cout << " 4 - Quit the program." << endl;
    cout << "Enter your selection now: ";
    cin >> first_input;
    //lets say if user enters value out of range. then want to show menu again.
    if(first_input > 4) {
        cout << "Invalid input "<<endl;
        continue;
    }
    // you can do other stuff here. 
    // ...
}while (first_input!=4)

You can try using goto label. 您可以尝试使用goto标签。

 do{ label: // your code if(check) goto label; }while(check); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM