Help with data protection

Started by
1 comment, last by mystb 15 years, 4 months ago
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.
--------- ApochPiQ : <Serious Grammar Nazi Pet Peeve> FFS guys, it's spelled "dying". That is all. </Serious Grammar Nazi Pet Peeve>
Advertisement
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.
or the easiest way... don't allow the player to type more than what you can handle :)
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].

This topic is closed to new replies.

Advertisement