cin loop problem

This is a discussion on cin loop problem within the C++ Programming forums, part of the General Programming Boards category; I have a nasty issue with 'cin' in loops. The first time through it works fine (I want to accept ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Location
    Auckland, New Zealand
    Posts
    3

    cin loop problem

    I have a nasty issue with 'cin' in loops. The first time through it works fine (I want to accept only an int for a function call in my program, and ignore chars that would mess it up) but subsequent 'cin' calls are ignored and the program loops indefinitely. Here's a short program to demonstrate:

    Code:
    #include <stdlib.h>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int input = 0;
    
      while(true)
      {    
        cout<<"Write something then press enter: \n";
        cin>> input; //<- Doesn't get called after the first time
        cin.ignore();
        if(input == 5)
          break;
        else
          cout<<"Invalid input.\n";
    
        system("PAUSE");
      }
    
      system("PAUSE");
      return EXIT_SUCCESS;
    }
    I've put in an extra system("PAUSE") call to stop it looping forever.
    Any suggestions on how to fix it?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Does it loop forever only when you input something other than a number? If so, then the problem is that cin goes into a fail state when the user inputs a non-digit character and you try to read a number. One common solution is to clear the failbit and ignore the offending characters in a loop. To change your current code, you could check cin.fail() in your if statement, so if input is 5 and !cin.fail(), then break. Otherwise call cin.clear(). You'd also probably want to ignore more than a single character. You could ignore some random large number like 1000: cin.ignore(1000, '\n'). You could also make sure to ignore everything and use:
    Code:
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    Make sure to #include <limits> and <ios>.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Location
    Auckland, New Zealand
    Posts
    3
    Hmm it's still looping away without waiting for user input (more specifically, the Enter key) whenever I input a char or string instead of a number (ie, when cin.fail() == true). It's like cin isn't even there. Just keeps printing my invalid input message and prompting for new input without waiting for the Enter key.
    Thanks for the cin.fail() tip though, that's really helpful.

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    beside ignoring bad input left on the stream you also have to clear the fail state of the stream with cin.clear()

    *edit* Just noticed Daved mentioned cin.clear...[I just looked at the code block]., but still you must have skipped it too. You need both the ignore and the clear.
    Last edited by Darryl; 07-03-2007 at 09:39 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with a loop
    By wiggsfly in forum C Programming
    Replies: 5
    Last Post: 11-30-2008, 12:45 PM
  2. whats the problem....
    By vapanchamukhi in forum C Programming
    Replies: 3
    Last Post: 09-05-2008, 01:19 PM
  3. while loop problem
    By chrismax2 in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2005, 12:21 PM
  4. While loop problem
    By nadkingcole in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 07:14 AM
  5. cin.getline and cin problems in a while loop
    By UnclePunker in forum C++ Programming
    Replies: 12
    Last Post: 05-07-2002, 10:43 AM

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