Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::cin.ignore() and std::cin.clear()

Reply
Thread Tools

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

 
 
Chris Mantoulidis
Guest
Posts: n/a
 
      01-06-2004
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
 
Reply With Quote
 
 
 
 
Jon Bell
Guest
Posts: n/a
 
      01-06-2004
In article <(E-Mail Removed) >,
Chris Mantoulidis <(E-Mail Removed)> 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 <(E-Mail Removed)> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      01-06-2004
On 6 Jan 2004 04:25:43 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) (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::streamsiz e>::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
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      01-06-2004
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


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      01-06-2004

"Jon Bell" <(E-Mail Removed)> wrote in message news:btecc8$ia0$(E-Mail Removed)...
> In article <(E-Mail Removed) >,
> Chris Mantoulidis <(E-Mail Removed)> 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).

 
Reply With Quote
 
Kevin Saff
Guest
Posts: n/a
 
      01-06-2004
"Chris Mantoulidis" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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++.




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 AM



Advertisments