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

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
[deleted]

@AquaMarine0421
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 repl.it share on practicing with binary search trees.

Original:

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.

Conclusion

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.