+ 1

how do I ensure that a user does not input a number less than 1 or greater than 10 in this code. int a; cin>>a;

26th Dec 2016, 6:56 AM
Cody Arthur
Cody Arthur - avatar
2 Answers
+ 7
a = 0; //a MUST be initialized so the while condition is true, else it will be skipped over. cout << "Please enter a number between 1 and 10:\t"; while (a < 1 || a > 10) { cin >> a; if (a < 1 || a > 10) { cout << "Error! Must be between 1 and 10!" << endl; } }
26th Dec 2016, 7:31 AM
Tamra
Tamra - avatar
+ 1
If (a is less than 1 OR a greater than 10) { Throw error on user } else { do stuff }
26th Dec 2016, 7:13 AM
Vishal++
Vishal++ - avatar