What's a better way to clear cin
input? I thought cin.clear
and cin.ignore
was a good way?
Code:
void clearInput()
{
cin.clear();
cin.ignore(1000,'\n');
//cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );
}
My teacher gave me this reply:
this is basically saying that your clearInput doesn't work FYI: ignore is NEVER a good idea as a way of getting rid of all that remains on a line
and your failing this test is exactly the reason why now go clear it the correct way
She has also told me this following:
on ignore you need to guess at how many characters you want to ignore If you absolutely know your data and it follows a strict format -- as in Excel spreadsheets, ignore is really good. If you are NOT in this type of file, then you NEVER use ignore. Question, is your data well-formed? If yes, use ignore. If not, don't use ignore.
List ALL the way of getting data? 1) there is extraction >>
and there is also??????
please list for me
The cin. clear() clears the error flag on cin (so that future I/O operations will work correctly), and then cin. ignore(10000, '\n') skips to the next newline (to ignore anything else on the same line as the non-number so that it does not cause another parse failure).
In order to clear the input buffer after the user has entered too many characters, you will need to clear the status flags of the input stream and then ignore all cahracters up to the newline. This can be done like so: cin. clear(); cin.
ignore() in C++ The ignore() function is used to ignore or discard the number of characters in the stream up to the given delimiter. It is a member function of std::basic_istream and inherited from input stream classes. It takes the number of characters and the delimiter character as arguments.
Using cin. You can use cin but the cin object will skip any leading white space (spaces, tabs, line breaks), then start reading when it comes to the first non-whitespace character and then stop reading when it comes to the next white space. In other words, it only reads in one word at a time.
Copy-pasting from the standard,
basic_istream<charT,traits>& ignore(streamsize n = 1, int_type delim = traits::eof());
Effects: Behaves as an unformatted input function (as described in 27.6.1.3, paragraph 1). After constructing a sentry object, extracts characters and discards them. Characters are extracted until any of the following occurs:
- if
n != numeric_limits<streamsize>::max()
(18.2.1),n
characters are extracted- end-of-file occurs on the input sequence (in which case the function calls
setstate(eofbit)
, which may throwios_base::failure
(27.4.4.3));c == delim
for the next available input characterc
(in which casec
is extracted).
You commented line with numeric_limits<>::max
is superior, but it looks like you didn't want to use something you didn't completely understand, which is also good.
The only thing someone could possibly want besides ignore
is non-blocking behavior, i.e. don't wait for the user to press return if the terminal is in unbuffered mode. But that's just entirely unsupported by iostreams as far as I know.
Your teacher’s reply are a bit unclear (at least to me).
Concerning ignore
, your teacher is wrong in principle: ignore
is the standard idiom of how to clear a stream (as shown by Potatocorn, this is even mentioned in the standard).
However, it’s important to notice that cin.ignore(1000)
is indeed a bad way of doing this: this just presumes that there won’t be more than 1000 characters in the buffer. Never use such a magic number in ignore
.
Instead, either use
cin.rdbuf()->in_avail()
(i.e. the available number of chars in the read buffer)1), or usenumeric_limits<streamsize>::max()
.1) Unfortunately, in_avail
is broken on some recent VC (?) compilers so this method isn’t very portable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With