cin infinite loop

This is a discussion on cin infinite loop within the C++ Programming forums, part of the General Programming Boards category; Hi, may someone tell me why the following code doesn't work when i insert a char instead of an integer? ...

  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,475
    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?
    I'm just trying to be a better person - My Name Is Earl

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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, 03: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, 11:21 AM
  5. infinite loop problem
    By Gil22 in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2003, 02:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21