Skip to content
How to return to another line of code in c++
Profile icon

I am programming in c++ and making a food ordering app, first it shows the menu, then you pick something, then I want the program to to return to the line of code that shows the menu and you input what else you want.

For example if I had 100 lines of code, perimeters were met at line 80 to go back to line 8, how would I do that? Please answer if you can so I can finish this project! (sorry if my problem did not make sense to you!)

You are viewing a single comment. View All
Answered by Geocube101 [earned 5 cycles]
View Answer
Profile icon

Hello BFDMod! I am not an expert coder, but I will tell you some solutions.
First of all, you can use goto(). goto is a function that helps you "go to" a part of your code. It is out-of-use, though, because using so many goto functions will harden your ability to distinguish how the algorithm flows.
Second of all, if you are not familiar with goto, then feel free to ask me anytime, I will try my best to solve your problem!

*edit: sorry! I thought you were programming in c for a sec. Goto is a function which is used in c, and I cannot think of a thing that does any similar thing in c++. I recommend you use c for this project, and if you just want to program in C++ then I will try my best to help you
:-) Best wishes, Aqua!

Profile icon

Thank you for your help, it is fine if you don't know how to do this, I will just find a work around for now because I have tried to figure it out for so long. I might take your advice for learning c later but for now im sticking with c++ because this is the 6th programming language i've learned and I am only 15 so i'll give it some time lol! Again thank you for taking the time to try and help me with my issue and I hope your next/current project goes as smooth as they really can!

Profile icon

it's generally bad practice to use goto, so try not to use it. You could try using a loop instead.

Profile icon

could you explain the basic syntax of a loop please?

Profile icon

its not relly smth about this but if you want, you can using namespace std; b4 int main and it'll let you not have to write std::@BFDMod

Profile icon

do not advise a new programmer to use GOTO statements. you do not want to touch that spaghet. There is only one case where it can be required in C, when you need to escape multiple nests of a for loop. Outside that, there is almost guaranteed to be a better way to write code.

Profile icon

thank you for your advice, I am also not that good at programming :)

Profile icon

Bad habits are hard to break. Better just to avoid them. Keep your head up. Never stop programming, and enjoy the journey. It's a worthy one that opens doors to possibility. Study lots of code, as much as you can. Over time you'll see things. I like to just practice on little snippets of code looking for better ways to tackle a problem. Sat down last night before going out for a walk and took a snippet from another piece of share on practicing with binary search trees.


void Tcase(Node** root){ int data = 0; while(1){ scanf("%d",&data); if(data==0) break; createBST(root,data); } }

It's not terrible code, but it's a little janky. So thought about trying to get rid of that silly break creating a little bit of spaghetti in the middle of the while.

Attempt 1

void Tcase(Node** root){ int data = 0; do { scanf("%d",&data); if(data != 0) createBST(root,data); } while (data != 0); }

This doesn't really improve much. Added two conditionals, but it does get rid of the break. Not really impressed with that one.

Attempt 2

void Tcase(Node** root){ int data = 0; do { scanf("%d",&data); if(data) createBST(root,data); } while (data != 0); }

The janky expression for the != test in the if expression. Just flipped that, but still didn't really improve much. Thought of maybe dumping the while and possibly messing around with a for loop instead.

Attempt 3

void Tcase(Node** root){ int data = 0; for(;;) { scanf("%d",&data); if(data) createBST(root,data); else break; } }

This forced me to put some of the break spaghetti back in, and in some ways made this worse than original.

Attempt 4

void Tcase(Node** root){ for(int data;;) { scanf("%d",&data); if(data) createBST(root,data); else break; } }

At least we can dump the declaration of the int and embed it into the for. But this still isn't really any better than the original. Really needed to dig deep. So looked at for about 10 minutes, wrote about another 5 or 6 variations... and arrived at this.

Attempt that was more acceptable...

void Tcase(Node** root){ for(int data = 1; !data;) { scanf("%d",&data); if(data) createBST(root,data); } }

This still requires two expression checks. We need to also set the value of data initially to 1 so the for loop doesn't bail on first loop. I'm still not happy with that. So woke up this morning and thinking maybe you can drop the conditional inside the for, and convert the other expression to a ternary.

Attempt to convert to a ternary if...

void Tcase(Node** root){ for(int data = 0;;) { scanf("%d",&data); (data) ? createBST(root,data) : break; } }

Unfortunately, you cannot put a break there in the way ternary ifs evaluate their parts... but you could do a janky space exploit and modify Attempt 4. Still dump the second evalution and get a single expression with an exit condition that happens at the tail. And you're not really adding complexity. Still the have the ability to clean up the code for simplicity.

Attempt 4 [shrunk]

void Tcase(Node** root){ for(int data = 0;;) { scanf("%d",&data); if(data) createBST(root,data); else break; } }

Could this still be improved? Is there a better way to mess around with this? I'm not a fan of compressing ifs like that. Is there limitations in the language? There are so many different ways to write code.


void Tcase(Node** root){ for(int data;;) { scanf("%d",&data); if(!data) break; createBST(root,data); } }

Write more code... lots of it. Pick it apart. I'm still not a fan of needing that break before adding a node. But it does imply the else. The while can still be dropped in place of a for with initialiser and the == 0 can be swapped for a ! check instead. This still doesn't address the validation of the scanf. Which is a real problem. But for a little coding exercise to mess around a little, it does help with understanding.