When you use cin object (cin >> var1) it returns istream &, so you can write cin >> var1 >> var2; but if it gets invalid input (e.g. chars instead of number), it returns NULL (0), and block itself. So command
if (!(cin >> input)) is true when input is ok (because it returns valid istream &), but when input is invalid, it returns NULL (== false).
When cin is blocked, any attempt to use it (cin >> input) fail (it does nothing). To unblock it, and using again you have to call clear function cin.clear(). But in this moment we still have chars in input stream, and we need them get away. That does this code:
while (cin.get()!= '\n');
cin.get(); reads a single char from input stream, and returns it, so we can test, if there is more chars or not (because the last is '\n'). While cycle has empty body, because command we need to do every loop if already part of the condition. It's just shorter entry of:
1 2 3 4
|
char Ch;
do
cin.get(&Ch); //reads a single char (or Ch = cin.get();)
while (Ch != '\n');
|
I hope I explain it (with my english...:-))