Thread: cin infinite loop

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    1

    cin infinite loop

    When you use cin to input an integer or double and the user inputs a character the program goes into an infinite loop. I have researched this online and seen that some have suggested using isdigit() isalpha() or isalphanum(), but i have tried these functions and they seem to only return zero since i am using doubles. How can i get around this, or alternately, is there another way to test that the input was a decimal number? Thanks to anyone who is willing to help a noob with his coding problem :P

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    I always recommend reading the data in as a string, validating it, and then converting to the appropriate type using a stringstream. Maybe this can be made into an FAQ if it isn't already?

    Code:
    # include <sstream>
    # include <iostream>
    # include <string>
    using namespace std;
    
    int main()
    {
        string strNum;
    
        // loop until valid input received
        while (true) {
            cout << "Enter a number: ";
    
            // get the number from standard input
            cin >> strNum;
    
            // look for the first character that does not match
            size_t pos = strNum.find_first_not_of("1234567890.-");
        
             // If we find a non-matching character, it's invalid 
             // so we retry
             if (pos != string::npos) {
                cout << "Invalid input, try again" << endl;
                continue;
            } 
            else break;
        }
    
        double d;
        stringstream ss;
    
        // One downside, but not the end of the world, is if the 
        // user enters a number with multiple decimal points, or 
        // a number with a minus sign in the wrong place, the 
        // double will be truncated at that point. But at least it
        // won't blow up. You can write more intelligent code to
        // check the input string if you want to.
        ss << strNum;
        ss >> d;
    
        cout << "You entered " << d << endl;
        return 0;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. infinite loop with signal chaining
    By zxcv in forum C Programming
    Replies: 1
    Last Post: 04-18-2008, 10:14 AM
  4. Stopping an Infinite Loop
    By linuxpyro in forum C Programming
    Replies: 4
    Last Post: 11-30-2006, 12:21 PM
  5. infinite loop problem
    By Gil22 in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2003, 03:24 PM