Fixing issues with cin.clear() & cin.ignore()

In this lesson I briefly touch upon cin.clear() & cin.ignore()

Let’s start a conversation about these functions in detail!

Who wants to start? :yum:

1 Like

From what I got from this video and doing a quick search on google cin.clear() is used to clear the remaining code and errors. While cin.ignore() will remove input contents in the code to prevent any errors.


If I understand it correctly, having entered a non-number value, we must both clear the error and ignore anything anything else in the input stream that might cause further errors.


After some poking around on Google, from my understanding, I want to add cin.ignore() can take parameters.
If you just use cin.ignore() as is, then it only skips the first character it reaches.
But if you try cin.ignore(1000, \n), then it will skip the next 1000 characters or skip until it hits a new line.
You can put different parameters in, it can be cin.ignore(50, ' '), this way it will skip the next 50 characters or skip until it hits a space.

1 Like

From what I understand in the video and what I’ve seen on the internet, what .clear(); does is to remove the errors in the data types entered, in other words is that if we put a character to an integer type this is an error and .clear(); eliminates that error. On the other hand, what .ignore(); does is ignore what is stored in the buffer to avoid reading errors.

1 Like

My understanding from the video is cin.clear() is used to clear a set line or number of characters, while cin.ignore() is meant to ignore any error codes or lines that are considered by c++ as an error.

i tried both with std::cin and without, doesn;t seem to work. so i have no idea what they do.! run tripplex and input 4 integers. run again and input 2 integers and they both give a result. using space between integers of course.

My understanding is that cin.clear() clears the error status flags and cin.ignore() removes all characters left in the stream up to the new line character. Effectively when used in combination following an input error they reinstate the stream buffer ready for new input.

Did you figure this out for yourself?

From your description that is correct behaviour as the code currently stands. Regardless of the number of space separated integers entered each loop of the code can only extract the first 3 in the buffer as we are assigning 3 variables, any others would be left in the buffer untouched ready to be processed in the second loop of the program. If you enter less than 3 (say ‘1 2’ and press enter) then user input will still require a third input before executing the next line in the program. Integer input is always valid when we are trying to assign the input to integer variables.

The problem comes when a user enters a non numeric character (for example ‘a’), this causes an error flag to be set when trying to assign it to the integer variable and the character is not removed from the buffer. This results in an endless loop because the buffer is never emptied and user input does not occur again until it is.

This is why we use the cin.clear() and cin.ignore() pair of functions, to clear the error status and remove the offending character from the buffer so user input can occur again and the endless loop is prevented.

I experimented with the cin.ignore function and I believe a better solution is to add the following parameters:

std::cin.ignore(256, '\n'); // removes all characters in the stream buffer up to the new line'

This will now remove all characters (limited to 256) up to the new line control character rather than just a single character as we previously had with no parameters. This has the added bonus of removing any extra valid characters (integers) entered over and above the three required ones, as highlighted by mortalwombat, as well as dealing with any erroneous alpha characters entered.


From my understanding, in this case, function std::cin.clear() clears the error where a character value is assigned to an integer variable, and function std::cin.ignore() clears the input stream buffer so that there is no other characters remaining to cause future error.

From my understanding and experimenting(removing the cin.ignore and clear).

It seems that cin.ignore, clears the error.
And also by using cin.clear it clears any other unnecessery letters to cause another error.

Because, when i removed those lines. And when I put letter into the program(to be an error).
It just became a infinite loop.

Also you can put in a digit in cin.ignore or clear, how much it can clear.

With my Google search, I found that cin.clear() clears any errors from the input and cin.ignore() will ignore invalid characters. Feel free to correct me if I misinterpreted anything.

I am unsure if my code is working properly, if I enter the digits, that are incorrect the code gives me the game over message and starts from the print intro section. However if i enter any letters the code runs through twice and prompts for input the third time. again starting from the into section. The while loop is
while (true) { PlayGame(); std::cin.clear(); std::cin.ignore(); }

EDIT; I added a value in the parenthesis as indicated by IanL, and that solved the issue i was having, as the program had to run through to clear for each alphabetic character entered previously.

Based on only a guess, cin.clear I think just removes an errors from appearing in the terminal and cin.ignore just doesn’t care and gets the value put in and plays it. If it’s wrong, its wrong and wont display any errors if anything but a number is put in?“¯_(ツ)_/¯“

In my opinion cin.clear() it role is clear The line that holds the answer and cin.ignore() it role is ignore the answer that is a character.