0

simple consol application BACK option [CPP]

How I can make a back option in my console application? I have 4 options in the program ( opt1, opt2..3..4) and, I need a back option, because if I choose an option, I don't want to exit my program, and I need choose another option. How I can make it?

1st Aug 2018, 7:43 PM
Martin Tálas
Martin Tálas - avatar
8 ответов
+ 1
I was the friend helping him and I think labels are not bad practice unless you start abusing them. In the case of a "back" option I find a do-while slightly more confusing than a simple "goto start;". Just because something *can* lead to bad code, that doesn't mean one mustn't use it :)
7th Aug 2018, 3:35 AM
Ratchy
Ratchy - avatar
+ 3
you can write cout statement at first to mention your criteria something like this: Enter 1 for option 1 ......and so on for all option... Enter 0 to quit.. Now, do while loop should be used to take input from user with the condition that user entered value is not zero.... Note : on sololearn, you need to give all input in one go seperated by space ... it would not ask interactively... You can test your code on pc or give all input on one go seperated by space
2nd Aug 2018, 1:44 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Thanks for the help. I'll try it. (I work with sololearn and Codeblocks)
2nd Aug 2018, 10:03 PM
Martin Tálas
Martin Tálas - avatar
+ 1
I could not solve it with do while loop, and another loops, but I got help from my friend. Easier with labels, than loop. I just use 2 labels.
7th Aug 2018, 12:11 AM
Martin Tálas
Martin Tálas - avatar
+ 1
Tálas Martin it's good to know that you have code which solves your purpose.. As a general practices, one should not use labels in code as far as possible... https://stackoverflow.com/questions/3517726/what-is-wrong-with-using-goto
7th Aug 2018, 1:48 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
it's not a harmful to use... you can go ahead with it.. to use or not is up to individuals... but IMHO, I prefer loops over labels..
7th Aug 2018, 4:12 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Thank you again, Ratchy :3
7th Aug 2018, 9:35 AM
Martin Tálas
Martin Tálas - avatar
0
Maybe you can push the answers to a stack. So once the user selects "back", then you can simply pop the last element from the stack. That way you returned to previous menu, keeping all the answers he/she gave before the last.
11th Apr 2019, 5:47 AM
Gyorgy Theisz
Gyorgy Theisz - avatar