Perhaps don't think about the limits for the time being. Instead focus on what happens here:
The user is supposed to enter an integer. Let's say for example they type "y" and then press the enter key.
The input buffer will contain the two characters "y\n".
In that case, the buffer could be cleared by
1 2
|
cin.clear();
cin.ignore(2, '\n');
|
But what if the user typed
that is, five spaces, a 'y', another five spaces and enter.
The leading spaces will be removed, input will stop at the 'y' because it is not an integer, so the buffer now contains seven characters from the 'y', to the '\n'.
In that case the buffer could be cleared by
1 2
|
cin.clear();
cin.ignore(7, '\n');
|
But we actually don't know what the user typed, so instead use some reasonably large number such as
cin.ignore(100, '\n');
or
cin.ignore(1000, '\n');
.
In practice those should be adequate.
The version with the value
numeric_limits<streamsize>::max()
is simply meant to cover all possibilities, it will ignore an unlimited amount of characters - or until the newline character is found.