Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading