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