well it will not ignore them because the type of variable "choice"{char} is compatible with the data you provided, probably i need to make things more clearer to avoid some confusion, this how it goes:
- the library difines an input stream for each primitive data type {ints,doubes,floats,char}
- a stream will only read data into a variable if it is compatible with the variable's data type or
the value available can be implicitly converted to the data type for example you can
read a char to a string, a double to an int and vice versa but you can't read a char into
an int or an integral type because there is no implicit conversion available, that's
a dead end and the stream doesn't know what to do so instead it will flag an error
to indicate something went wrong somewhere.
Basically three error states can be set
1. when the stream hits the end-of -file marker the {eofbit} is set.
2. when the stream encounters an error of invalid types like i described above the
{failbit} is set;
3. when the stream encounters a system level error a {badbit} is set.
in all of those you can recover from the 1st two by clearing the stream and continue using it like in the example i gave you, the last error
is unrecoverable and rarely occurs.
1 2 3 4 5 6 7 8
|
if(!cin)///here the stream provides an implicit conversion to a bool that allows you to
{ /// enquire about it's usage state {goodbit} will return true while the above bits
///returns false;
cin.clear();///this sets the goodbit
cin.ignore(100,'\n');///this tells the stream to ignore 100 characters that are in the
///stream buffer or any characters less that hundred but before the
///'\n' character is encountered.
}///the stream can then be used again.
|