Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you set a maximum limit to an integer (C++)?

Tags:

c++

If I never want an integer to go over 100, is there any simple way to make sure that the integer never exceeds 100, regardless of how much the user adds to it?

For example,

50 + 40 = 90
50 + 50 = 100
50 + 60 = 100
50 + 90 = 100
like image 898
Max O'Neill Avatar asked Nov 30 '22 07:11

Max O'Neill


1 Answers

Try this:

std::min(50 + 40, 100);
std::min(50 + 50, 100);
std::min(50 + 60, 100);
std::min(50 + 90, 100);

http://www.cplusplus.com/reference/algorithm/min/

Another option would be to use this after each operation:

if (answer > 100) answer = 100;
like image 194
xthexder Avatar answered Dec 02 '22 21:12

xthexder