Jump to content
  • Advertisement
Sign in to follow this  
link161

A switch case...

This topic is 4200 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
Advertisement
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
Sign in to follow this  

  • 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!