mwahi
mwahi

Reputation: 5

How do I make an Int in C++ have a limit?

Is there any way I could ensure an int doesn't cross a certain limit? and if it needs to (if the program is adding numbers to the int and it crosses the limit) it goes back to 0 and does the job from there?

Upvotes: 0

Views: 111

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409356

It sounds like you want the modulo operator %.

If the upper limit is limit then

value = value % limit;  // Or value %= limit

will "reset" the value back to zero if it's about to pass the limit.

Upvotes: 3

Related Questions