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

  • Thread starter Chris Mantoulidis
  • Start date
C

Chris Mantoulidis

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
 
J

Jon Bell

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().
 
T

tom_usenet

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
 
O

osmium

Chris said:
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
 
R

Ron Natalie

Jon Bell said:
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).
 
K

Kevin Saff

Chris Mantoulidis said:
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++.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top