Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 08:39 PM
Knuut Olsen-Solberg
Guest
 
Posts: n/a
Default Empty the inbuffer

(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?

  #2  
Old July 19th, 2005, 08:39 PM
Ivan Vecerina
Guest
 
Posts: n/a
Default Re: Empty the inbuffer

Hi,
"Knuut Olsen-Solberg" <knut@iet.hist.no> wrote in message
news:3F9A7E0C.1080805@iet.hist.no...[color=blue]
> Is there some function for emptying the inbuffer?
> Look at this:
>
> #include <iostream.h>
> #include <conio.h>[/color]
Note: <iostream> is standard C++, not <iostream.h>
[color=blue]
> 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?[/color]

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
--
http://ivan.vecerina.com
http://www.brainbench.com <> Brainbench MVP for C++


  #3  
Old July 19th, 2005, 08:39 PM
Knuut Olsen-Solberg
Guest
 
Posts: n/a
Default Re: Empty the inbuffer


Ivan Vecerina wrote:[color=blue]
> 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 ?[/color]

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...

 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles