Why would we call cin.clear() and cin.ignore() after reading input in C++?


In C++ the cin is used to take input from user. Sometimes for some reasons some error flags are set. In that time the cin does not take any input. Sometimes it takes some other characters. So if we clear the cin, then the error flags are reset. Then we can use getline(), get() etc. functions.

The ignore() function is another stream input function. If we write the function like this

cin.ignore(1000, ‘\n’)

Then it will ignore next 1000 characters, otherwise ignore lines until ‘\n’ is found.

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements