I cant figure out how to enter anything else if i screw up. Im supposed to be able to enter a choice and if it is right then terminate the loop and continue, but if i enter a string or something then im supposed to be able to re enter a choice but i cant figure out how to do that.
I wouldn't consider myself adept in C++ just yet, and there may very well be a more efficient way to handle this.
Well look at the link I posted.
There are several problems with your code (if you don't mind me pointing them out)
It is : int main() not void main() always and for ever. Are you using a really ancient compiler like turbo C++ 3.0 which allows this despite all the C / C++ standards?
Although there are many valid reasons for using infinite loops, I am wary of beginners who use them routinely, mainly out of laziness. Your code is a classic misuse: it doesn't have a break statement - so it is a true infinite loop.
I can understand why people use a return statement to return early from a void function - but I have never understood why people put one at the end.
It is more usual to have a break statement at the end of each case in a switch - you are using return instead. It is not wrong, but could cause problems later when you modify the code.
while( .... && valid(selection) ) would be a lot less messy or terrible than refusing to write a function and consequently putting those 20 checks inside the while loop.
Now I know you have vastly more knowledge & experience than me, but I cannot help arguing this (or at least enquiring why you think your solution is better). I am always ready to learn something new, but I don't understand why you are saying this at the moment.
The checks inside the while loop are the switch statement with a default case. What would your valid function look like?
Which is worse:
1. a relatively complicated while condition with a function call while( std::cout << "Please make a selection -> " << std::endl && valid(selection) )
2. very simple conditions while(!Quit) and switch(input)
IMO it is simple, scalable & straight forward.
giblit's last code snippet is almost the same as what I had in the first place.
Stroustrup talks about keeping things simple in this section:
Prefer highly structured code
It isn’t enough to be disciplined in our specification of
data structures and interfaces: we must also simplify our
code logic. Complicated control structures are as danger-
ous to efficiency and correctness as are complicated data
I don't mean to be trite - but isn't my code a good example of that?