Suppose you have an application that, as things go by, asks the user questions ...
int main() {
std::cout << "Espere..." << std::endl;
// aqui hace una (quiza larga) operacion y de repente pregunta
std::cout << "Elija una opcion? [A,B,C]" << std::endl;
std::cin >> opcion;
// y sigue con la operacion en base a la opcion
}
In such a case, it would be important to clean the input buffer, because during the wait, the user could touch the keyboard without the intention, and this accidental touch has remained in the buffer until the standard input is read (cin) .
It is a design decision, when you want to be (something more) sure that it is a conscious decision of the user and therefore you do not accept the buffer data.
Then before cin >> opcion
you set cin.ignore(N)
, to discard N bytes from the input buffer, forcibly.
Finally, I'm not sure there is a portable solution to all platforms, this is the base. There are other more options of the c ++ style.
cin.clear();
cin.ignore(INT_MAX);