• Advertisement

#### Archived

This topic is now archived and is closed to further replies.

# Can't continue in a switch/case

This topic is 5994 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

I''m having trouble trying to be able to loop the menu I made without having to use an exit option. I''ve tried using coninue, but the compiler keeps telling me I use it illegally, but I used it after my default statement because break wasn''t working. Here''s what I have: switch (option) { case 1: cout << "Statement\n"; break; case 2: cout << "Statement\n"; break; default: cout << "Statement\n"; break; } When I run the program, it breaks out after hitting Enter no matter what the input is. So how can I make it loop while the input is !option? And why won''t it accept continue in place of that last break? I tried using various loops, but if I don''t selecet a valid option, it loops forever. Thank you

#### Share this post

##### Share on other sites
Advertisement
I''m trying to figure out why you would want to use a continue in a switch. Continue only re-evaluates the condition but if I remember correctly, it can only be used with for, if and while. Again, I might be wrong though. Anyhow, try to give a bit more code so we can try to understand what you''re saying...

"And that''s the bottom line cause I said so!"

Cyberdrek
Headhunter Soft
A division of DLC Multimedia

Resist Windows XP''s Invasive Production Activation Technology!

"gitty up" -- Kramer

#### Share this post

##### Share on other sites
do something like

do(
int go = 0;

switch( exp)
{
case 1:
break;
case 2:
go = 1;
break;
}

} while( go == 1);

#### Share this post

##### Share on other sites
From language reference

continue;

The continue keyword passes control to the next iteration of the smallest enclosing do, for, or while statement in which it appears.

#### Share this post

##### Share on other sites
[EDIT: As Stoffel most kindly pointed out below, my suggestion will not work, so just ignore this whole post]

Possibly the simplest method:

  do { option = get_option(); // Whatever you do to get it.... switch (option) { case 1: // DO something... break; case 2: // Do something else... break; default: continue; };} while (0);

The only way to loop is to execute the continue-statement in the default-branch. All other case labels execute a break, that takes them out of the switch and the do-while is also terminated (because 0 is always false). Perhaps not the best description, but it should work as you expect.

Edited by - Dactylos on September 24, 2001 1:28:07 PM

#### Share this post

##### Share on other sites
Thank you for the replies.

What I mean is, it doesn''t matter what I input, I only get one chance and the program breaks out of the switch.
I want to make it so that if the user doesn''t choose one of the options listed, it won''t break until they have chosen a valid option.

The way it is now, if my options are 1 and 2, and then it will default if anything other than 1 or 2 is chosen, but if anything else is chosen, then the program doesn''t continue until 1 or 2 is pressed, instead it ends there just the same.

Grr.. now I''m confusing myself. Sory if I''m not being clear enough on this. I''m just trying to remain in the switch until a valid option is chosen, instead of breaking out anyway. Continue isn''t doing what I thought it should for some reason.

Thank you

#### Share this post

##### Share on other sites
int cH;
while(cH != 4)
{
cout<<"*****Choose*****\n";
cout<<"1. Create Charecter\n2. Participate in Tournament\n3. Train with The Great Kaio\n4. Quit\n\n";
cin>>cH;
if(cH == 1)
{
//do stuff;
}

when cH doesnt equal four you are able to chose.

Edited by - Drizzt DoUrden on September 23, 2001 8:44:09 PM

#### Share this post

##### Share on other sites
Hi,

You can also try this

while(1)
{
switch(x) { case 1 : /*do something break*/; case 2 : return 0;}
}

Also
int i = 1;
while(i)
{
switch(x) { case 1 : /*do something*/ break; case 2 : i = 0; break; }

I am posting this from lynx so the text prolly will be unformatted. So bear with me

#### Share this post

##### Share on other sites
quote:
Original post by Tempest
I''m just trying to remain in the switch until a valid option is chosen, instead of breaking out anyway.

First, looping is handled by for or while statements (which are essentially the same thing, but that''s another thread), so you need to place your switch statement inside a loop.
bool not_valid = true;//...while( not_valid ){// read input here; put in option  switch( option )  {  case 1:    //...    not_valid = false;    break;  case 2:    //...    not_valid = false;    break;  // more options...  default: break;  }}

#### Share this post

##### Share on other sites
u can also use simple ''goto'' statement like this:

int a;
enter: cin>>a;
switch(a)
{case 1:break;
case 2:break;
default:goto enter;
}

of course, this isn''t so recommended

#### Share this post

##### Share on other sites
[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

#### Share this post

##### Share on other sites
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

#### Share this post

##### Share on other sites
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

#### Share this post

##### Share on other sites
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

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites
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~

#### Share this post

##### Share on other sites
Ah, alright. I''ll try that. I didn''t think about making it char. Thanks.

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### Popular Tags

• Advertisement
• ### Popular Now

• 10
• 14
• 11
• 10
• 11
• Advertisement