+ 1

Setting a limit

How do I set a limit for a variable that will be entered by the user using cin statement. int a; cin >> a; how do I set a limit of 40 for the value entered for the variable a

28th Jul 2017, 5:41 AM
Ibrahim
Ibrahim - avatar
1 Answer
+ 9
int a; do { cin >> a; if (a > 40) std::cout << "Over limit! Try again\n"; } while (a > 40); // or simply cin >> a; if (a > 40) a = 40; // proceed.
28th Jul 2017, 6:10 AM
Hatsy Rei
Hatsy Rei - avatar