Thread: Formatted input: Detecting numeric overflow?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    399

    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. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    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.
    Last edited by Lesshardtofind; 09-25-2010 at 04:55 PM.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Secure coding and formatted input
    By laserlight in forum C++ Programming
    Replies: 7
    Last Post: 12-16-2009, 10:33 PM
  2. Never find EOF after improperly formatted input
    By MALDATA in forum C Programming
    Replies: 5
    Last Post: 09-30-2008, 01:24 PM
  3. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM
  4. Pausing for input, with kbhit() still detecting keys
    By Da-Spit in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2002, 05:04 AM
  5. formatted file input
    By Flikm in forum C++ Programming
    Replies: 1
    Last Post: 04-09-2002, 10:12 PM