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.

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.

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.