As its name already suggests, the cin.ignore()
is used when you want to ignore one or more of the buffer incoming.
When exactly to use Cin.ignore() in software written in C++?
The cin.ignore()
treats as delimiter the characters: \t
and \n
, then you will use when receiving an entry and want to ignore those characters that remained in the buffer incoming.
So, what’s the right way to use Cin.ignore() ?
In a well summarized way, you use in cases that want to ignore unread characters on buffer as I mentioned earlier.
Ex:
You want to get one string
user and store in a char
:
char teste[20];
You will read the entry using the cin
:
cin >> teste;
Assuming the user has typed teste
and pressed ENTER. The teste
will be received, and the \n
will remain in the buffer incoming.
Using the cin.ignore()
the character \n
who remain in the buffer input will be ignored.
I tried to be as brief as possible in the answer, that reference may be a complement to your understanding.
One thread similar
– Tuxpilgrim