# Preventing overflow

• 03-18-2011
843
Preventing overflow
Suppose I want to accept input of an int variable. I would like the program to quit if the number input is beyond the range of the int data type. What would be the best way to do this?
• 03-18-2011
kmdv
It depends on the input of the variable. Can it be represented as a hexadecimal value? Can it be negative? Assuming length of input string can be formatted (0x, etc) and inifite, no pure and simple solution comes to my mind.

You can load this into a larger data type and then check, but I think that the best solution would be a custom string parser.
• 03-18-2011
843
This is what I tried. The number is an unsigned long, and for some reason, this conditional statement is not triggered.

Code:

```    if (x < 0 || x > 4294967295) {         cout <<" Number has to be from 0 to 4,294,967,295.";         return -1;     }```
• 03-18-2011
kmdv
Because size of long = int in this case.

Think, if x is unsigned, how can it be less than 0..
• 03-18-2011
843
Yeah, I was trying to restrict the possible size of input to be processed, but still, I don't know what to do if something like a negative number is input. Something beyond cin has to intercept the input...
• 03-18-2011
anon
You can first read this into a string.

Then perform a conversion using a stringstream. Then you can perform all kinds of checks on the input.

Code:

```#include <string> #include <iostream> #include <sstream> #include <cctype> bool read_ulong(unsigned long& out) {     std::string input;     std::cin >> input;     if (input[0] == '-') { //streams will happily read negative values into unsigned types         return false;     }     std::stringstream ss(input);     if (! (ss >> out) ) {  //input is not a number at all         return false;     }     if (!ss.eof()) {         char ch;         ss >> ch;         return !isdigit(ch);  //at least one unread digit - overflow     }     return true; } int main() {     unsigned long n;     while (true) {         std::cout << "Enter unsigned integer: ";         if (read_ulong(n)) {             std::cout << n << '\n';             break;         }         else {             std::cout << "Bad input\n";         }     } }```
Example run:
Code:

```Enter unsigned integer: abc Bad input Enter unsigned integer: -123 Bad input Enter unsigned integer: 123456678901234567890 Bad input Enter unsigned integer: 453.232 453```
As you can see, this truncates rational numbers, which you can also report as an error or accept.
• 03-18-2011
843
Thanks! I would use this, but I wish there was a simpler way which only uses the string and iostream libraries.
• 03-18-2011
whiteflags
Quote:

Originally Posted by 843
Thanks! I would use this, but I wish there was a simpler way which only uses the string and iostream libraries.

Technically speaking, that's all what anon showed you really is. stringstream derived from the iosbase object. Not really what you're asking for, but that's the truth.

You could always use strtoul(), which does actually report overflow errors, specifically.