Sign in to follow this  
Dark_Glitch

Help with data protection

Recommended Posts

Ok so I'm creating a few functions to use in a game someday and I'm wondering something: How would I make a check that would automatically set an entered value to the max of its data type? For instance, I have an unsigned int money. In a console program I have entered a number well over the maximum that an unsigned int can handle, how do I force the program to take a value that is that large and automatically set it as the unsigned int max? Thanks for reading.

Share this post


Link to post
Share on other sites
Store it temporarily in an unsigned long long? Then use pow(256, sizeof(unsigned int))-1, or std::numeric_limits<unsigned int>::max(); to get the maximum number an unsigned int can hold.

Share this post


Link to post
Share on other sites
Or, you could just use the correct library function which already does all that for you.

Quote:
These functions shall return the converted value, if any.
If no conversion could be performed, zero shall be returned.
If the correct value is outside the range of representable values, {INTMAX_MAX}, {INTMAX_MIN}, or {UINTMAX_MAX} shall be returned (according to the return type and sign of the value, if any), and errno shall be set to [ERANGE].

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this