Unavoidable goto?
After few minutes of thinking, I failed to find a way to prevent a goto in this piece of code. Is it possible? Maybe whole thing should be re-written?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
MainMenuLabel:
MainMenu myMenu;
int _mode = myMenu.Show(MenuWindow, "MainMenu.png");
if(_mode == 2 || _mode == 3)
return 0;
if(_mode == 0)
{
MainMenu _myMenu;
_playFirst = _myMenu.Show(MenuWindow, "SPMenu.png");
if(_playFirst == 3)
return 0;
else if(_playFirst == 2)//If Back to menu is clicked, go back
goto MainMenuLabel;
MainMenu __myMenu;
if(_level == 3)
return 0;
_level = __myMenu.Show(MenuWindow, "Level.png");
MenuWindow.Close();
}
//..
Game myGame(_mode, _playFirst, _level);
|
1 2
|
else
(_playFirst == 2) ? goto MainMenuLabel : goto someOtherLabel;
|
not shore if that would work,
however goto shold be generaly avoided in programs.
use function instead.
EDIT:
or even better use switch statement
Last edited on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
while(1)
{
MainMenu myMenu;
int _mode = myMenu.Show(MenuWindow, "MainMenu.png");
if(_mode == 2 || _mode == 3)
return 0; // leave while loop (and whole function)
if(_mode == 0)
{
MainMenu _myMenu;
_playFirst = _myMenu.Show(MenuWindow, "SPMenu.png");
if(_playFirst == 3)
return 0; // leave while loop (and whole function)
else if(_playFirst == 2)//If Back to menu is clicked, go back
{ // do nothing, whole while loop will repeat }
else
{
MainMenu __myMenu;
if(_level == 3)
return 0; // leave while loop (and whole function)
_level = __myMenu.Show(MenuWindow, "Level.png");
MenuWindow.Close();
break; // leave while loop
}
}
}
|
Last edited on
@Moschops
Wow... That looks pretty good. With small modification fits perfectly into program.
Thanks alot.
Topic archived. No new replies allowed.