Many thanks for your generous and useful responses.I have found your comments useful even when it did not appear to deal with the question.
I have spent some trying to understand the code provided. I still need clarification on some of the comments and will attempt to follow the order in which the comments appear above from time to time as I complete the examination of the code samples.
*********************************************************************
@helios:
First the question: theory deals with things like: which header is required, what problems can be anticipated for a particular implementation (An example the known problem with cin buffer retention requiring a flush, another example would be whether an implementation can trigger an unexpected loop).So theory is about the rules concerning how things work.The other comments, including your second comment, represent the theory.
Code samples do show how things are done but not why. I learn better and faster and remember more if I know why things are done.
The code you supplied shows "while(true)" and embedded "return true" in an if statement. It seems to me that this will generate a continuous loop, so a break statement might be required. In addition I am not sure how the function (action to be undertaken when the if statement returns true) is "seen" in the if statement I need your guidance here.
You stated "This is not possible, at least not with any functionality provided by standard libraries." It is in fact possible and very effective. As I understands it "cin" is an iostream object and therefore very much from the Standard Library. The applicable code with #include <limits> could be:
if (! cin)
{
cout << "Only numbers are allowed.\n";
cin.clear();
system("pause");
cin.ignore(std::numeric_limits<streamsize>::max(), '\n') ;
} |
The above code works efficiently at the end of a for(;;;) in which a switch statement is embedded with all of the above embedded in try/catch operating in main().
An improved code sample for the above would be appreciated.