Hello to everyone,
I have a question: there is something similar to the label goto? I don't want to use it because I know that is not very appreciate and at the same time in the programme that I wrote there are lots of menus and I don't want to use others do while.
Thanks you in advance
Note that there is one valid use case for goto in C++; when you need to break out of deeply nested loops. Java has labeled break/continue, but C++ does not, so goto is a necessary evil in this one specific case.
And if I would like to come back of only one level in a series ( two or more) of nested switch case, which is the solution? How can I apply the do while loop substituting goto's ?
There are possible errors but it is not the point, I want just to show my situation and try to make better this code without using goto but I find very difficult going back from the submenus to the previous levels of the entire menu.
Of course there are instructions in every case of the switches but I rase them because the code was too long
It seems a great idea!
However I've just one question: what would you suggest to come back to the superior level menu from a sub-menu: just calling the function of the superior level menu or using instructions such as return?
After some trials I found out that the first option seems working whereas the second one seems not.
Thanks of course for the help
You should return to go back up out of menus. Don't call the function of the menu you want to go to, that just eats up more memory as you use the program longer.
However I've just one question: what would you suggest to come back to the superior level menu from a sub-menu: just calling the function of the superior level menu or using instructions such as return?
After some trials I found out that the first option seems working whereas the second one seems not.
Thanks of course for the help
I'd either create a function or class for each menu.
Here's the function version, it's much more simple and shorter than the other one.
Note: I used recursion here in a bad way, see my next post for a better alternative.
Okey, yeah, it makes sense, if i were to stay in the main menu for to long I'd reach a pretty deep recursion level and therefore the call stack would grow very big...
That works, but I like to use a more state-machine-like approach, where each function returns the next function to be called: https://ideone.com/DUZONx (designed to run without interactive input)
Don't use the word 'sub', it distracts from what is really going on.
Gamer2015 wrote:
I mean, you don't know the previous menu with that approach, right?
The functions in my example can take any parameters, if the previous menu is important for some reason you can pass it as a parameter (though really it shouldn't be important).
I think that's not up to you to decide and he clearly stated that he wanted that:
amidaraxar wrote:
what would you suggest to come back to the superior level menu from a sub-menu
LB wrote:
The functions in my example can take any parameters, if the previous menu is important for some reason you can pass it as a parameter
yeah, that's true, but wouldn't this cause the call stack to grow if you'd call the parent menu instead of just letting the function end?
You also said:
You should return to go back up out of menus.
Wouldn't that violate your own advice?
But well, I think OP allready found what he searched for
I think that's not up to you to decide and he clearly stated that he wanted that:
It is up to me if I want to give my personal opinion on what I believe proper design is. You don't come to a forum asking for people to tell you what you already believe.
Gamer2015 wrote:
yeah, that's true, but wouldn't this cause the call stack to grow if you'd call the parent menu instead of just letting the function end?
You also said: "You should return to go back up out of menus."
Wouldn't that violate your own advice?
The functions do not call each other, instead they return a std::function object which gets called by main. Please read the code more closely before criticising me.
The functions do not call each other, instead they return a std::function object which gets called by main. Please read the code more closely before criticising me.
I'm sorry, but I can't figure out how you can have any number of parent menus that way, could you show me?
It doesn't even require any parameters to be passed around: https://ideone.com/Bbv93E
Try running it yourself in your own IDE. Step through it in the debugger if you're still confused.