0
C++
How to restrict input of type int to some range. Like if i wanna like to enter input from 1 to 4, any other will show a message of âerrorâ
1 Answer
+ 3
int n;
do {
cin.clear();
cin.ignore();
cin >> n;
} while (cin.fail() || n<1 || n>4);
This would be a loop that will only stop after n is between 1 and 4.
With ifs you could fill in some error communication.