Empty the inbuffer

  • Thread starter Knuut Olsen-Solberg
  • Start date
K

Knuut Olsen-Solberg

(Windows XP, Borland C++ Builder5 Console program.)

Is there some function for emptying the inbuffer?
Look at this:

#include <iostream.h>
#include <conio.h>
int main()
{
double Number;
char Ch;

cout << "Number: ";
cin >> Number; // The '\n' is left in the buffer

cout << "Char: ";
cin.get(Ch); // will take the '\n' and the program does not stop.

cout << "\nNumber: " << Number << " Char: " << Ch << " END\n";

cout << "\n\nPress a button..."; getch();
return 0;
}


Here we are preceded by a cin >> Number, and the user might have written
"2.3", or "2.3abcd" We could use cin.ignore(80, '\n'), but what if we
generally wanted to be sure the inbuffer is empty?
 
I

Ivan Vecerina

Hi,
Knuut Olsen-Solberg said:
Is there some function for emptying the inbuffer?
Look at this:

#include <iostream.h>
#include <conio.h>
Note: said:
int main()
{
double Number;
char Ch;

cout << "Number: ";
cin >> Number; // The '\n' is left in the buffer

cout << "Char: ";
cin.get(Ch); // will take the '\n' and the program does not stop.

cout << "\nNumber: " << Number << " Char: " << Ch << " END\n";

cout << "\n\nPress a button..."; getch();
return 0;
}

Here we are preceded by a cin >> Number, and the user might have written
"2.3", or "2.3abcd" We could use cin.ignore(80, '\n'), but what if we
generally wanted to be sure the inbuffer is empty?

C++ has no portable concept of an 'in buffer' as you describe it.
What if the input of the program was redirected, and didn't come
from the console ?

If you want to ignore the rest of the line, the right approach is
indeed:
cin.ignore( numeric_limits<int>::max() , '\n' );

The following will skip all input until the end of the file:
cin.ignore( numeric_limits<int>::max() );
But this will fail in console mode, as the function will
keep waiting for more input to be ignored.

Your specific implementation might have a way to empty a specific
input buffer (e.g. keyboard buffer).
Or, since you are already using the non-portable conio.h header,
you could try something like:
while( kbhit() ) getch(); // platform-specific...


Cheers,
Ivan
 
K

Knuut Olsen-Solberg

Ivan said:
C++ has no portable concept of an 'in buffer' as you describe it.
What if the input of the program was redirected, and didn't come
from the console ?

Thanks.
I did not know that. For me it seemed natural that the program made an
in buffer in memory when running one of the buffered functions e.g.
cin.get(). In my mind this buffer might be used even if the input was
redirected.
I would have thought that may be each function got its own buffer
throughout the program...
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top