# Thread: Formatted input: Detecting numeric overflow?

1. ## Formatted input: Detecting numeric overflow?

I couldn't find anything about how the formatted input operator handles numeric overflows. For example, how can you detect an overflow in the following code:
Code:
int x;
std::cin >> x;

2. Lol I tried to find the solution for you looked around for about 2hrs. I see alot of ways to test if a multiplication or addition is above limits using.

Code:
if((x*x) > std::numeric_limits<int>::max())
but I couldn't find any way to combine this with
Code:
std::cin >> x;
So my only guess would be putting the input into a string or larger bit variable like a double and then checking if it's greater than the int max. Such as
Code:
#include <limits>
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
double a;
std::cout<<" max int value = "<<std::numeric_limits<int>::max()<<endl;
std::cin>>a;
while((a) > std::numeric_limits<int>::max() )
{
std::cout << "overflow" << std::endl;
std::cout << "please input a non overflow number."<<endl;
std::cin>>a;
}
else
std::cout<< "Your number is within the limits of an int"<<endl;

int b = a;
std::cout<<b<<endl;
system("pause");
return 0;
}
I'm sure this wouldn't be the most efficient method, but I've tried alot of combinations and the only way to glitch it out that I've found is inputing a character instead of a number, or going over the limits of double. (which a string would probably solve, but of course the code would get a little lengthy.) Of course this obviously doesn't check for a negative overflow either.

3. There is some discussion on the matter here: C++ Standard Library Active Issues List

It seems that failbit gets set on numeric overflow, but there's no way to explicitly check for overflows.