> Is the mission of cin.clear() on line 35 to reset any error flags on the stream?
Yes.
> If so, why is this done?
If the expression in
if( std::cin >> a >> b ) is evaluated as
false, the attempt to read two numbers failed; the stream is put into a failed state.
https://stdcxx.apache.org/doc/stdlibug/29-1.html
The stream in a failed state can't perform more input/output operations till it is put back into a good state.
> cin.ignore ( 1000, '\n' ) Stops extracting characters if the end-of-file is reached?
> Or does it empty all the input memory?
It extracts and discards (up to a maximum of 1000) characters from the input buffer till a new line is extracted and thrown away. One reason why this is required is that there might have been an input error, and we should throw away the junk remaining in the input buffer.
This is required even if there was no error in input is because we want to throw away the new line left in the buffer after the formatted input, before we attempt unformatted input with
std::cin.get() ;
To understand why:
http://www.cplusplus.com/forum/general/69685/#msg372532
> cin.get() will take some information from the cin input stream.
std::cin.get() retrieves the next character (unformatted input) from the input buffer as an
int; unformatted input means that it does not skip white space characters.
If the stream has reached end of file (nothing more can be read), it returns
EOF.
http://www.cplusplus.com/reference/istream/istream/get/
If the next character was '|' or we got an
EOF, we exit from the loop to end the program.