By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,463 Members | 3,115 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,463 IT Pros & Developers. It's quick & easy.

Clearing input buffer

P: n/a
Hello all, may I ask a question?

Having just started C++ (using microsoft visual studio), I am using cin/cout
for console applicarions (yes I know it doesn't happen in the real world!)
and I'm finding it difficult to track down a method of clearing the input
buffer after a cin (to get rid of potential rubbish).

cin.flush() has been mentioned, but isn't an option on visual studio and
cin.ignore() requires you to specify number of chars to ignore.

Any help much appreciated.

Regards

Carol
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"Carol Pedder" <ca***@pedder002.freeserve.co.uk> wrote in message
news:cl**********@news5.svr.pol.co.uk...
| Hello all, may I ask a question?

Sure.

| Having just started C++ (using microsoft visual studio), I am using cin/cout
| for console applicarions

Well, I don't know what an 'applicarion' is, but let's
see if we can work it out <g>.

| (yes I know it doesn't happen in the real world!) and

What do you mean by that ?

| I'm finding it difficult to track down a method of
| clearing the input buffer after a cin (to get rid of
| potential rubbish).

std::cin.clear();

....will reset the stream state back to a good state.

You can then follow that call with:

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

....to remove all remaining junk from the stream.

It should now be ready for another read operation.

| cin.flush() has been mentioned, but isn't an option on visual studio and

It's not an option, because the C++ standard does not define
such an operation for an input stream.

| cin.ignore() requires you to specify number of chars to ignore.

Not always - You can use the default call:
cin.ignore();

....which will ignore '1' character from the stream,
if one exists.

[snip]

Cheers.
Chris Val
Jul 22 '05 #2

P: n/a
Carol Pedder wrote:
Hello all, may I ask a question?

Having just started C++ (using microsoft visual studio), I am using cin/cout
for console applicarions (yes I know it doesn't happen in the real world!)
and I'm finding it difficult to track down a method of clearing the input
buffer after a cin (to get rid of potential rubbish).

cin.flush() has been mentioned, but isn't an option on visual studio and
cin.ignore() requires you to specify number of chars to ignore.

cin.ignore() with '\n' as the terminal character, assuming you want to
remoove the '\n' that remains in the stream.

E.g. cin.ignore(1000, '\n');

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #3

P: n/a
Carol Pedder wrote:

cin.flush() has been mentioned, but isn't an option on visual studio and
cin.ignore() requires you to specify number of chars to ignore.

flush() does not affect the input side of streams. If it's been mentioned
they mentioners were wrong.

cin.ignore works, it takes both a count and a delimeter.

In actuality, what you think you're asking for is probably impossible.
There's no concept of a "non-blocking" input in standard C++, that is,
there's no way to tell that there isn't pending unread input.

So in a trully portable program, you can't rely on that methodology.
The best you can hope for is to read up to the next newline or other
sentinal character when you want to discard input.
Jul 22 '05 #4

P: n/a

Ron Natalie wrote:

In actuality, what you think you're asking for is probably impossible.
There's no concept of a "non-blocking" input in standard C++, that is,
there's no way to tell that there isn't pending unread input.

So in a trully portable program, you can't rely on that methodology.
The best you can hope for is to read up to the next newline or other
sentinal character when you want to discard input.


Thanks for all your replies. I think I am asking the impossible.

What I really wanted to do was clear the input buffer in the event of trying
to input a letter to an integer variable, which sends the program into a
loop as it attempts to extract an integer from the buffer ad infinitum.

I realise that it is much better practice to use getch() and validate input
properly, but could not understand why the output buffer can be flushed, but
not the input buffer - I still can't, but I guess that's just the way it
works!

Thanks again

Carol
Jul 22 '05 #5

P: n/a
Carol Pedder wrote:
Thanks for all your replies. I think I am asking the impossible.

What I really wanted to do was clear the input buffer in the event of trying
to input a letter to an integer variable, which sends the program into a
loop as it attempts to extract an integer from the buffer ad infinitum.

I realise that it is much better practice to use getch() and validate input
properly, but could not understand why the output buffer can be flushed, but
not the input buffer - I still can't, but I guess that's just the way it
works!

Check this sample code from TC++PL 3, page 620:

void read_a_line(int max)
{
// ...
if (cin.fail()) { // Oops: bad input format
cin.clear() ; // clear the input flags (21.3.3)
cin.ignore(max,;) ; // skip to semicolon

if (!cin) {
// oops: we reached the end of the stream
}
else if (cin.gcount()==max) {
// oops: read max characters
}
else {
// found and discarded the semicolon
}
}
}

So if you want to guard against invalid input and remove the remaining
'\n' and other trash from the stream, one approach you can follow is this:

while(cin)
// ...
if(cin.fail())
{
cin.clear();
cin.ignore(1000, '\n')
}
//If you want to detect the end of file
if(cin.eof())
// ...

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #6

P: n/a
Ioannis Vranos wrote:
So if you want to guard against invalid input and remove the remaining
'\n' and other trash from the stream, one approach you can follow is this:

..... if(cin.fail())
{
cin.clear();
cin.ignore(1000, '\n')
}


Placing this code after my cin does the trick - a perfect solution, thanks a
lot.

Regards

Carol
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.