Clearing istream

This is a discussion on Clearing istream within the C++ Programming forums, part of the General Programming Boards category; Does anyone know a way of clearing the istream to stop the program skipping cin lines which should pause to ...

  1. #1
    cplus
    Guest

    Unhappy Clearing istream

    Does anyone know a way of clearing the istream to stop the program skipping cin lines which should pause to accept data. I've tried using cin.ignore(256,'\n'), but I don't want the user to press enter all the time to clear the buffer stream.

    I've tried cin.ignore(256) but that just freezes the program, any ideas?

    Thanks.

  2. #2
    cplus
    Guest
    If your reading this can you please post something, any ideas at all. I really need some help on this.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    try this....

    if ( cin.fail() )
    {
    cin.clear();
    cin.ignore (256,'\n');
    // either get input again or go back to get input etc. depends on how your code is structured
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    cplus
    Guest
    Is there any chance that you could explain what cin.fail is checking for e.g. over loaded buffer stream?, what does cin.clear do?

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    a stream is said to have a state.
    there are four flags you can check that tell you the state of the stream.

    good
    bad
    fail
    eof

    good can be checked with cin.good() .This will return true if no input error occured.
    bad can be checked with cin.bad() .This returns true if a FATAL error has occured.
    fail can be checked with cin.fail() .This returns true if a recoverable error has occurred.
    eof can be checked with cin.eof() . This returns true if the end of file has been reached.For a keyboard and windows/dos os that is ctrl-z.

    cin.clear() clears the state of the stream. This basically means you are clearing the failbit so that the cin.ignore will work. Input ops on a failed stream do not work.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    cplus
    Guest
    Thank you very much. Trying to find information like that in the borland help files is hopeless.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    194

    Re: Clearing istream

    Originally posted by cplus
    Does anyone know a way of clearing the istream to stop the program skipping cin lines which should pause to accept data. I've tried using cin.ignore(256,'\n'), but I don't want the user to press enter all the time to clear the buffer stream.

    I've tried cin.ignore(256) but that just freezes the program, any ideas?

    Thanks.
    Do you mean it appears that the program skips over some cin>> lines?
    If you mix input of numbers, and strings/chars you may get problems.
    Say you tell the user to input a number.

    int x;
    cout << "Enter a int value : ";
    cin >> x;

    Then you ask them to enter some text

    char buffer[100];
    cout << "What is your first name : ";
    cin.getline(buffer,99);

    The problem is that when they entered the number in the first cin statement, the \n from pressing enter is still sitting in the input buffer, and the call to getline sees that \n and stops there.
    You need go call cin.ignore(100,'\n'); after inputting numbers before you switch to using getline.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. istream
    By Beowolf in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2007, 06:11 PM
  2. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  3. ostream and istream
    By xddxogm3 in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2003, 08:36 AM
  4. istream >> overloading
    By wazza13 in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2002, 11:56 PM
  5. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 04:07 PM

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