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.

3 Likes

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.

1 Like

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.

[EDIT]
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.