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

From: tom_usenet (tom_usenet_at_hotmail.com)
Date: 01/06/04


Date: Tue, 06 Jan 2004 13:24:24 +0000

On 6 Jan 2004 04:25:43 -0800, cmad_x@yahoo.com (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



Relevant Pages

  • Re: Clearing the input buffer - then pausing
    ... >> once (preceeded by zero or more other characters). ... >> do not comment out the first cin line. ... >> method does not operate on a new buffer, ... > it processes a stream of characters that might come from a pipe (via OS ...
    (microsoft.public.vc.language)
  • Re: Clearing the input buffer - then pausing
    ... program at some arbitrary point and resume only when the Enter key is pressed once (preceeded by zero or more other characters). ... The code below works if I do not comment out the first cin line. ... What appears to be happening is that the ignore method does not operate on a new buffer, ... Firstly, you have to remember that std::cin is just a stream interface - it processes a stream of characters that might come from a pipe, or from a file, or from the console; std::cin can't itself tell which it is getting. ...
    (microsoft.public.vc.language)
  • Re: cin seems to behave strange...
    ... > int main{ ... characters at your prompt, the program will exhibit undefined behavior. ... > I would have expected that I get asked for one string press enter (after ... > second cin completely! ...
    (alt.comp.lang.learn.c-cpp)
  • Re: is cin always the keyboards input?
    ... the whole point of 'cin' is to keep this detail hidden from you. ... > input stream) as cin. ... testdatamaker outputs strings every second, ...
    (comp.lang.cpp)
  • Re: is cin always the keyboards input?
    ... the whole point of 'cin' is to keep this detail hidden from you. ... >> cat file | myapp ... Set stdin to non-blocking mode ... Here the stream will be in an error state. ...
    (comp.lang.cpp)