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