Well it sort of works but only with integers... The problem is that as I soon as I put a character or string of sort as a response it goes haywire and loops infinitely.... I guess it is that i'm not sure how I would make it detect a character has been detected... Oh and If you put to big of a number as well it crashes as well and loops forever...
Maybe but I just want them to have to input a number ya know? That is why I put it on a infinite loop so that they have to in order to break free.
But if you throw , they won't get another opportunity. C++ exceptions have termination semantics.
stringstream are a better way to handle this.
If you want to investigate exceptions, then try a different example like Latitude and Longitude. Throw if either of these is out of bounds. Negative Latitude means South, while negative Longitude means West,or if you want use two enum for N , S, and E , W and keep the numbers positive.
Why don't you just continue in the else part avoid using exceptions altogether (a good thing if you can).
By testing cin you have avoided all the other errors, except the input being a char. A char would normally fall outside the range of 1 to 8 (on the keyboard ), unless you did manage to somehow send it one in that range. The earlier problems were due to you using a bitwise & instead of boolean &&
One example of where exceptions can be more useful in a class constructor, in the case of an invariant not being met.
More typical usage of exceptions involves #include <exception> , so one can use all the built in exceptions, and derive you own ones.
So one needs to be careful to use exceptions appropriately, some avoid them altogether.
It makes no sense to throw and catch within the same place. why not line 25 -> 19?
You problem is that cin goes into an error state if you do not enter a number. This state remains (and thus number is not changed anymore). In this case you need to call clear() and ignore():