fork download
  1. #include <iostream>
  2. #include <limits>
  3. using namespace std;
  4.  
  5. int main() {
  6. int choice = 0;
  7. cout << "Enter your choice 1, 2, 3: ";
  8. cin >> choice;
  9. cout << endl;
  10. while (cin.fail() || choice <= 0 || choice >= 4) { // check input value
  11. cin.clear();
  12. cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  13. cout << "Wrong input value! Please enter only 1, 2, 3: ";
  14. cin >> choice;
  15. cout << endl;
  16. }
  17. }
Success #stdin #stdout 0s 3464KB
stdin
12rc
0
a
11
$
10

2
stdout
Enter your choice 1, 2, 3: 
Wrong input value! Please enter only 1, 2, 3: 
Wrong input value! Please enter only 1, 2, 3: 
Wrong input value! Please enter only 1, 2, 3: 
Wrong input value! Please enter only 1, 2, 3: 
Wrong input value! Please enter only 1, 2, 3: 
Wrong input value! Please enter only 1, 2, 3: