如何在开关中使用while循环以防止无效输入?

时间:2017-07-15 17:29:39

标签: while-loop switch-statement

我不知道如何包含“检查器”以防止输入错误请帮助:( 对不起,刚开始学习昨晚原谅我.. 我应该怎么做,不使用if else语句或何时应该使用它呢?

#include <iostream>
#include <iomanip>
using namespace std;
int main() 
{
int get_number;
char ch;
cout << "Hey! What is your number 1 OR 2?! :";
cin >> get_number;

switch (get_number)
{       
    case 1: cout << "You pressed 1/nPress anykey to continue!";
    break;

    case 2: cout << "You pressed 2/nPress anykey to continue!";
    break;

    default: cout << "Retry! 1 OR 2?! :";
        cin >> get_number;
}

cin.ignore(1, '/n');
cout << "Press any key to continue";
cin.get(ch);

return 0;
}

0 个答案:

没有答案