std::cin.ignore() and std::cin.clear()

Discussion in 'C++' started by Chris Mantoulidis, Jan 6, 2004.

  1. Let's say I have this:

    std::string s1;
    std::cin >> s1;

    This will read s1 from cin until it finds a space (or a newline,
    whichever comes first).

    Okay this works. But when I want to continue reading it reads what's
    left over in the cin, and well that's logical.

    At first I thought that std::cin.clear() would sort that out, but it
    didn't... So what does clear() do anyway, if not clear all cin data?

    I looked it some places and saw that ignore is what I needed...

    std::ignore(1000, '\n'); (or something bigger than 1000 characters).

    However I don't like this very much... What if there were 10^100 other
    characters in the input before the new line (this isn't possible I
    guess but I'm just trying to explain why I don't like that way).

    And in some examples I see the use of clear() but still I can't
    understand what it does.

    Thanks in advance,
    cmad
     
    Chris Mantoulidis, Jan 6, 2004
    #1
    1. Advertising

  2. Chris Mantoulidis

    Jon Bell Guest

    In article <>,
    Chris Mantoulidis <> wrote:
    >
    >At first I thought that std::cin.clear() would sort that out, but it
    >didn't... So what does clear() do anyway, if not clear all cin data?


    clear() resets the stream status flags. For example, when the stream
    encounters an error (e.g. it wants to read an int and you give it
    non-numeric characters), it sets a failure flag. Even after you skip over
    the bad data with ignore() you can't continue reading until you reset the
    flags with clear().

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
     
    Jon Bell, Jan 6, 2004
    #2
    1. Advertising

  3. Chris Mantoulidis

    tom_usenet Guest

    On 6 Jan 2004 04:25:43 -0800, (Chris Mantoulidis)
    wrote:

    >Let's say I have this:
    >
    >std::string s1;
    >std::cin >> s1;
    >
    >This will read s1 from cin until it finds a space (or a newline,
    >whichever comes first).
    >
    >Okay this works. But when I want to continue reading it reads what's
    >left over in the cin, and well that's logical.
    >
    >At first I thought that std::cin.clear() would sort that out, but it
    >didn't... So what does clear() do anyway, if not clear all cin data?


    It clears the error state of the stream. If it's at eof(), or the last
    operation failed (and set fail()), then you can call clear() to set
    the state back to good() so that you can continue using the stream
    (all operations fail when a stream isn't good()).

    >
    >I looked it some places and saw that ignore is what I needed...
    >
    >std::ignore(1000, '\n'); (or something bigger than 1000 characters).
    >
    >However I don't like this very much... What if there were 10^100 other
    >characters in the input before the new line (this isn't possible I
    >guess but I'm just trying to explain why I don't like that way).


    I can see why you don't like that, but the correct code (by the new
    2003 update to the standard) is

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    The extracts an unlimited amount of characters (the max is a sentinel
    value). A bit verbose, but you can always write a little inline
    function.

    Tom

    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
     
    tom_usenet, Jan 6, 2004
    #3
  4. Chris Mantoulidis

    osmium Guest

    Chris Mantoulidis writes:

    > Let's say I have this:
    >
    > std::string s1;
    > std::cin >> s1;
    >
    > This will read s1 from cin until it finds a space (or a newline,
    > whichever comes first).
    >
    > Okay this works. But when I want to continue reading it reads what's
    > left over in the cin, and well that's logical.
    >
    > At first I thought that std::cin.clear() would sort that out, but it
    > didn't... So what does clear() do anyway, if not clear all cin data?
    >
    > I looked it some places and saw that ignore is what I needed...
    >
    > std::ignore(1000, '\n'); (or something bigger than 1000 characters).
    >
    > However I don't like this very much... What if there were 10^100 other
    > characters in the input before the new line (this isn't possible I
    > guess but I'm just trying to explain why I don't like that way).
    >
    > And in some examples I see the use of clear() but still I can't
    > understand what it does.


    Take a look at this. Although I warn in the message that it is from memory
    no one objected to it, so it must have been right.

    http://makeashorterlink.com/?X13E228F6
     
    osmium, Jan 6, 2004
    #4
  5. Chris Mantoulidis

    Ron Natalie Guest

    "Jon Bell" <> wrote in message news:btecc8$ia0$...
    > In article <>,
    > Chris Mantoulidis <> wrote:
    > >
    > >At first I thought that std::cin.clear() would sort that out, but it
    > >didn't... So what does clear() do anyway, if not clear all cin data?

    >
    > clear() resets the stream status flags. For example, when the stream
    > encounters an error (e.g. it wants to read an int and you give it
    > non-numeric characters), it sets a failure flag. Even after you skip over
    > the bad data with ignore() you can't continue reading until you reset the
    > flags with clear().


    Actually, you probably want to clear the errors before calling ignore. Ignore
    could fail itself (such as hitting the end of file).
     
    Ron Natalie, Jan 6, 2004
    #5
  6. Chris Mantoulidis

    Kevin Saff Guest

    "Chris Mantoulidis" <> wrote in message
    news:...
    > At first I thought that std::cin.clear() would sort that out, but it
    > didn't... So what does clear() do anyway, if not clear all cin data?


    Good question. Stream.clear() resets the error state of Stream. For
    unfortunate historical reasons, Stream.clear (Foobit) has the effect of
    resetting the error state to Foobit (and only Foobit), not of clearing
    Foobit. Likewise, Stream.setstate (Foobit) does not actually set the error
    state to Foobit, but logically adds the bit to the error state. These are
    probably the most poorly named functions in C++.
     
    Kevin Saff, Jan 6, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,108
    P.J. Plauger
    Oct 17, 2003
  2. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,700
    Alex Vinokur
    Mar 6, 2005
  3. puzzlecracker
    Replies:
    3
    Views:
    1,774
    Mike Wahler
    May 8, 2006
  4. =?ISO-8859-15?Q?Juli=E1n?= Albo

    cin.ignore... better way wanted...

    =?ISO-8859-15?Q?Juli=E1n?= Albo, Oct 29, 2006, in forum: C++
    Replies:
    6
    Views:
    416
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Oct 29, 2006
  5. Fernando
    Replies:
    4
    Views:
    1,685
    Fernando
    Nov 16, 2011
Loading...

Share This Page