Jump to content
  • Advertisement

Archived

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

Tempest

Can't continue in a switch/case

This topic is 6168 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


Link to 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


Link to post
Share on other sites
Guest Anonymous Poster
do something like

do(
int go = 0;

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

} while( go == 1);

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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


Link to 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


Link to 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


Link to 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


Link to 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


Link to 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


Link to post
Share on other sites
Guest Anonymous Poster
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


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!