+ 2
C++ cin explain please
How can I understand it? : int n; while (cin.fail()) { cin.clear(); cin.ignore(999, '\n'); cout << "*** Incorrect operation, please try again! ***" << endl << "Input: "; cin >> n; } please can someone explain what do cii.fail() , cin.ignore() and cin.clear() mean?
1 ответ
+ 1
fail() : Returns true if the failbit and/or badbit is set for a given stream. The failbit is a state flag used to indicate I/O errors and tream close failures. The badbit is a state flag used to indicate errors in validation of the stream object, or failure in file handle retrieval, etc. These bits are present for both input and output streams.
clear() : This clears all the bits that are set. There are 3 state flag bits in a stream: eofbit (end of file), failbit (I/O), and badbit (stream validity, file id retrieval errors, etc.). Clear resets the state of all 3 of these bits to false. The stream may still not be usable after clear().
ignore() : This ignores or discards the last n characters from the stream' buffer matching to a given character. The original prototype is ignore(number_of_chars, del_char). By default, the values of number_of_chars is 1 and del_char is \n. This way, ignore discards the last newline from the stream's buffer.