Sign in to follow this  

A switch case...

This topic is 3864 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 have this very simple game that is run by a switch case and in one of the choices I wanted there to be a little mini-game type thing that requires another switch, My qustion is, is that a alright to do? is it bad technique or habit? and if so what would you guys suggest?

Share this post


Link to post
Share on other sites
For a simple game, the main loop being a switch statement isn't at all a far-fetched thing. When you get into nesting them, though, you should probably wrap up that inner switch into a function. This'll make the code infinitely easier to read and maintain, as well as prepare you for nastier coding down the road. So:
{
// main loop:
switch (choice)
{
case 0:
// buy menu
cin >> buyWhat;
switch (buyWhat)
{
case 0:
// buy thing 0
break;
case 1:
// buy thing 1
break;
// ...
}
break;
case 1:
// sell menu
cin >> sellWhat;
switch (sellWhat)
{
case 0:
// sell thing 0
break;
case 1:
// sell thing 1
break;
// ...
}
}
}

becomes:

{
// main loop
switch (choice)
{
case 0:
buyMenu( );
break;
case 1:
sellMenu( );
break;
// ...
}
}

void buyMenu(void)
{
// buy menu
int buyWhat;
cin >> buyWhat;
switch (buyWhat)
{
case 0:
// buy thing 0
break;
case 1:
// buy thing 1
break;
}
}

void sellMenu(void)
{
// etc
}

Instead of the buy/sell menus, you'd have another function for your minigame(s).

Best of luck!
-jouley

Share this post


Link to post
Share on other sites

This topic is 3864 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this