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

  • Thread starter =?ISO-8859-15?Q?Juli=E1n?= Albo
  • Start date
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

There has got to be some way to just say 'if there is anything in the
input stream or buffer get rid of it' so I can start fresh so to speak.

Maybe what you really want is "ignore until end of line"? You can use for
example:

while (cin && cin.get () != '\n')
continue;

The continue is just for clarity, an empty loop body leads to confusion.

But IMO the better way when you want input delimited by lines is to read
always with getline, and then parse the lines individually. Mixing << and
getline or ignore is confusing and error prone.
 
S

sandy

I seem to ALWAYS need to use cin.ignore in my programs (console apps
using Dev C++ for a University course).

The problem is that if I don't ignore ENOUGH characters then it's like
not having the cin.ignore. If I ignore too many that's just as bad.

There has got to be some way to just say 'if there is anything in the
input stream or buffer get rid of it' so I can start fresh so to speak.

What is it? (It has just GOT to exist...)

The things I don't know about C++ could fill volumes! Oh, wait... they
already do ;)

Thanks.
 
P

Peter Jansson

I seem to ALWAYS need to use cin.ignore in my programs (console apps
using Dev C++ for a University course).

The problem is that if I don't ignore ENOUGH characters then it's like
not having the cin.ignore. If I ignore too many that's just as bad.

There has got to be some way to just say 'if there is anything in the
input stream or buffer get rid of it' so I can start fresh so to speak.

What is it? (It has just GOT to exist...)

The things I don't know about C++ could fill volumes! Oh, wait... they
already do ;)

Thanks.

How about replacing
"if there is anything in the input stream or buffer get rid of it"
with
"ignore the standard input stream, i.e. don't read from it at all"
?

Sincerely,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
 
M

Mike Wahler

I seem to ALWAYS need to use cin.ignore in my programs (console apps
using Dev C++ for a University course).

The problem is that if I don't ignore ENOUGH characters then it's like
not having the cin.ignore. If I ignore too many that's just as bad.

There has got to be some way to just say 'if there is anything in the
input stream or buffer get rid of it' so I can start fresh so to speak.

What is it? (It has just GOT to exist...)

The things I don't know about C++ could fill volumes! Oh, wait... they
already do ;)

std::cin.ignore(std::numeric_limits<std::streamsize>::max);

extracts and discards characters from stream until eof or error.

-Mike
 
D

David Harmon

On 29 Oct 2006 10:21:52 -0800 in comp.lang.c++, "(e-mail address removed)"
There has got to be some way to just say 'if there is anything in the
input stream or buffer get rid of it' so I can start fresh so to speak.

I don't know how you got stuck in that pattern... I venture to guess
that most programs never use .ignore().

Many of my console programs go like:
Get a line of input with std:getline(cin, somestring);
Parse somestring, decide what to do with it (error msg if nonsense.)
Repeat until getline fails.

Often I use boost::regex to do the parsing - http://boost.org
 
F

Frederick Gotham

=?ISO-8859-15?Q?Juli=E1n?= Albo:
while (cin && cin.get () != '\n')
continue;

The continue is just for clarity, an empty loop body leads to confusion.


If you think there's confusion, you have the choice of using comments.

while (cin && cin.get() != '\n') /* Empty Body */;


On the other hand, you could just trust that the reader knows how a loop
works.
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Frederick said:
If you think there's confusion, you have the choice of using comments.

And I also have the choice of do it like I do.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top