Can't continue in a switch/case

Started by
16 comments, last by Tempest 22 years, 6 months ago
[EDIT: As Stoffel most kindly pointed out below, my suggestion will not work, so just ignore this whole post]

I still say that my version is the simplest to accomplish what you need
(Just look a few posts up, just above the post where you asked the second time).
I think that it will accomplish exactly what you need with the least amount of extra code. Simply enlose the input-retrieving function and switch-statement in a do-while(false) loop (look at the code, and it should be evident how it works).

Edited by - Dactylos on September 24, 2001 1:28:58 PM
Advertisement
Dactylos, that will also only run once. The condition is checked when you do a continue, and while (0) is always a failure.

I think the problem is that the poster is confused on the uses of continue and break.

break is used to immediately abort the current for, while, do or switch.

continue is used to immediately jump to the "check condition" part of the current for, while or do. It doesn't apply to switch.

On my compiler, if I try to write a program with just a single switch and a continue in it, the compiler tells me "illegal continue". I therefore assume that if your program is compiling, your switch is within a for, while or do loop. _That_ is what is being continued, not your switch.

The solutions suggested by the first AP and Oluseyi are the way to go here.

Edited by - Stoffel on September 24, 2001 1:05:38 PM
quote:
Stoffel said:
Dactylos, that will also only run once. The condition is checked when you do a continue, and while (0) is always a failure

Ah, of course it does. How ebarrassing.
I was so used to thinking in terms in "continue jumps to the top of the loop" instead of thinking that it jumps to the condition-test.

Edited by - Dactylos on September 24, 2001 1:29:39 PM
Hi folks. Thanks again. I''m still having trouble though.

I can''t seem to find a way out of the loop now. My computer gets hung up and loops continually if the switch statement defaults. I''ve tried every combination of loops and exits I can think of. I can''t figure out why I''m having so much trouble with something that seems so simple.
Sould I just forget about using switch and just use if statements? It''s not what i really want to do, but I''m ready to give up.

Thanks again
Hey! I just found something interesting out. It seems that when the input doesn''t give the same results, well, that sounds funny. What I mean is, My options are 1 and two, then default should handle everything else. But what I realized is that if I input any other number (1,2,3,56...) it does as I expect, but for some reason, if I use letters, then the program just gets hung up in a loop.

Any ideas on this? I would think that any key pressed that is not a valid selection would prompt the default just as the invalid numbers do.

Thanks.
Make sure you allow the user to enter a new choice after he hits the wrong one (like including your cin inside the loop, or providing a new cin in the default case, right before you hit the continue line).

As for the second problem, use a char instead of int. A user can enter a number or letter into a char without the program going berzerk on you, while a int cannot. With a char, you can still test the same way for numbers like you did before, and if the user enters a letter, it''ll just hit the default case like you want.


*ahem*
~All your base are belong to us~
Ah, alright. I''ll try that. I didn''t think about making it char. Thanks.
quote:Original post by SirSmoke
As for the second problem, use a char instead of int. A user can enter a number or letter into a char without the program going berzerk on you, while a int cannot. With a char, you can still test the same way for numbers like you did before, and if the user enters a letter, it''ll just hit the default case like you want.


Actually, you can enter a letter into an int.
int c = getch(); // this works 


I suspect the problem is in how he''s comparing the characters in his switch:
switch( option ){case 1: // integer comparison  //...  break;case ''q'': // character comparison  //...  break;case "q": // string comparison - ERROR!  //...  break;default: break;} 

The above code is good.

This topic is closed to new replies.

Advertisement