Thread: cin infinite loop

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    60

    cin infinite loop

    Hi,
    may someone tell me why the following code doesn't work when i insert a char instead of an integer? What should i do?
    Code:
    #include <iostream>
    using namespace std;
    int main(){
        int x;
        while(true){cout<<"Insert an integer\n";
        if(cin>>x){
        cout<<"number is valid\n";
        break;}
        else{cout<<"number is invalid.Try again\n";cin.clear();}}
        system("pause");
        return 0;}

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    You have instructed the computer to read an integer, not a char variable.
    Although the compiler will reconize a char as a small byte integer.
    In what way will it "not work"?
    Do you get an error message, or is it a logical error?
    Double Helix STL

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    If i insert a char instead of an integer the program enters in an infinite loop.
    Shouldn't the clear() function clear the input buffer so as to be able to read a new input?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You should also remove the wrong char from the stream
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int x;
        while(true)
    	{
    		cout<<"Insert an integer\n";
    		if(cin>>x)
    		{
    			cout<<"number is valid\n";
    			break;
    		}
    		else
    		{
    			cout<<"number is invalid.Try again\n";
    			cin.clear();
    			cin.ignore();		}
    	}
        system("pause");
        return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by tezcatlipooca
    If i insert a char instead of an integer the program enters in an infinite loop.
    Shouldn't the clear() function clear the input buffer so as to be able to read a new input?
    clear clers the wrong state flags, not the content of the stream
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> cin.ignore();
    Note that this will only ignore a single character. If the user types a paragraph, it will only ignore the first letter, then run the loop again and output the error message, then ignore another character, and so on.

    What you probably want is to ignore everything up until the user hit enter to submit their input:
    Code:
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    You have to #include <limits> and <ios> for numeric_limits and streamsize. You could also just put in some big number like 1000 which will work 99.99% of the time anyway since users rarely will type more than 1000 characters at a prompt.

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