cin.ignore and clear

hello, i wonder about cin.clear(); cin.ignore(10000, '\n'); I want to know i understand these two functions. I think cin.clear, deletes all that is in the buffer? is this correct? and the ignore, collects all in the buffer up to 10000 characters and replaces them with \n ? this is correct? I see these operations together when reading throgh sample code, i wonder, why do they go together, if clear, deletes all in the buffer, why would the ignore be needed?
I think cin.clear, deletes all that is in the buffer? is this correct?

No that is not correct.

and the ignore, collects all in the buffer up to 10000 characters and replaces them with \n

No this is also incorrect.

I see these operations together when reading throgh sample code, i wonder, why do they go together, if clear, deletes all in the buffer, why would the ignore be needed?

See the above answers.

Did you even try to find and read some actual documentation of these standard functions?

std::istream.clear() doesn't do anything to any buffer, it modifies the stream state flag.

std::istream.ignore() doesn't do any kind of replacement. It just reads and discards characters from the stream.

The two are usually used together because if a stream is in an error state no further processing can happen until the error state is cleared. Once the error state is cleared if there is "bad" data in the stream that "bad" data must be discarded before you try to retrieve the desired information again.

No.
Not even close.
Did you even look up the definitions of the functions?
Don't just guess and then ask other people if your guess is correct!
Those two statements can be used when you have detected an invalid line of input from the user that you want to discard so that you can then go on to read more input.

Whenever a read operation fails cin will enter an "error mode". All read operations will automatically fail while in the error mode. The input that it failed on still remains in the buffer. What cin.clear() does is that it leaves the error mode so that you can read (or discard) input again. cin.ignore(10000, '\n') discards all characters up to and including the next newline character, or 10000 characters, whichever is fewest.
yes i look up the definintions but they use other words which i do not undeerstand, so hard for me to understand what they mean. And other expinations from sites like stackoverflow use to much hard terminology for me to understand. like this "Sets the stream error state flags by assigning them the value of state" this does not help me understnad

The ignore says it discards, so it collects all 10000 in buffer then discards but then why is /n there, it is just there for new line then? not to be replace?
Last edited on
Hello MyOnlinePersona,

I will give this a try.

No and No.

The stream "cin" and "cout" along with file streams have state flags or bits that keep track of the streams. These are the "good", "eof", "fail" and "bad" bits according to http://www.cplusplus.com/reference/ios/basic_ios/good/

The "cin.clear()" function just resets these bits when one or more have been set.

The line cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>. also seen as cin.ignore(1000, '\n');. Clears the input buffer for the number of characters provided by the first parameter or until it finds the second parameter whichever comes first.

The first example is the more portable way of obtaining a large number based on the header files and compiler used. Either one will work.

You will find this most often used with cin >> num; where "num" is defined as a numeric variable and the formatted input is expecting a number to be entered, so if a non numeric value is entered, e.g., a letter, "cin" will fail and the ".clear()" function will reset the state bits followed be the ".ignore()" to clear the input buffer before you try to use "cin" again.

Andy
Hello Handy Andy thank you very much this was very helpful, i did not know about state flags, i now understand why use cin.clear, thank you for expalining this way, much better than what i have read before.
Hello MyOnlinePersona,

You are welcome.

I would not discount what jlb, dutch and Peter87 have said Although some of that information is a more technical answer it is still good to learn what they are saying.

It may need updating, but the "reference" section here is always a good place to start.

Andy
I am sorry peter i did not see your comment , i must have missed because i commented. thank you for that help too.

and yes handy andy, i will try and understand the reference more
Topic archived. No new replies allowed.