cin is how C++ interacts with
standard input, which is essentially a buffer (storage, like you said) where the user or system feeds things into, and then get processed by your program.
When something goes wrong with this input stream, it gets put into a bad state where it will no longer accept input.
cin.clear() clears any failure flags in the cin stream. So, OK, now cin will accept more input and once again attempt to feed that input into your variables.
But there could still be junk in your stream's buffer. For example, if you type in "blahdkdf dfadsf badfdslj", the first
cin >> number;
consumed the "blahdkdf" word, but the rest of the text is still in cin's buffer, waiting to be used.
That's where the
cin.ignore() call comes in.
numeric_limits<streamsize>::max()
is saying "ignore up to the maximum possible size that the stream itself could be", just in case the user typed a really long message in.
'\n'
is saying ignore (discard) up until the next newline in the buffer, which would happen when the user presses enter to send the text.
So together, it clears any bad/remaining input from the previous thing you typed, to put your input stream in a fresh state.
_________________________________________
I just meant that, in general, one disadvantage of macros is that they can be less type-safe if not implemented carefully.
1 2 3 4 5 6 7 8 9 10 11
|
// Example program
#include <iostream>
#include <climits>
#include <limits>
int main()
{
std::cout << sizeof(CHAR_MAX) << '\n';
std::cout << sizeof(std::numeric_limits<char>::max()) << '\n';
}
|
will output:
because CHAR_MAX is just macro for an int literal, while std::numeric_limits<char>::max() is an actual char that is the maximum value that a char can be.
<limits> can also be used with templates easier, because you can insert any type into numeric_limits<T>.
[A lot of C++ features are like this; a bit more verbose than C but also safer and more extendable.]