C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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”

17th Mar 2019, 6:56 PM
alien ock
alien ock - avatar
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.
17th Mar 2019, 7:11 PM
HonFu
HonFu - avatar