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