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.
Ok but that still doesnt answer my question, if i enter the wrong thing then the program quits on me how can i keep it from doing that? here is my code
1 2 3 4 5 6 7 8 9 10 11 12
cin >> select;
switch(select)
{
case 1:
break;
case 2:
break;
default:
cout << "Not a valid input" << endl;
}
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.
Ok but that still doesnt answer my question, if i enter the wrong thing then the program quits on me how can i keep it from doing that?
If you look at my code you will see that the switch is inside a bool controlled while loop. It also has a quit option in the menu with a corresponding case in the switch.
I meant 1 then 2 and yeah sorry I meant && not ||.
He only had two options thats why i said that..
He could of also done something like this if this is perferred..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
bool temp( false );
while( std::cout << "Please make a selection -> " << std::endl && std::cin >> select && !temp )
{
switch( select )
{
case 1: case 2:
temp = true;
break;
default:
std::cin.clear();
std::cin.ingore( std::numeric_limits<std::streamsize>::max() , '\n' );
std::cout << "That is not a valid selection." << std::endl;
break;
}
}
Not sure if this is any better though but a little neater I suppose.
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.
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:
Stroustrup wrote:
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
structures.
I don't mean to be trite - but isn't my code a good example of that?