Ok, I have searched for the past 15mins and cant find anything. I kind of remember doing this but cant get the syntax right. What I want is to prompt the user if he wants to run the program again and execute based on the Y or N.
Kind of like this, but forgot the rest.
cout << "Would you like to run the program again? (Y/N) ";
cin >> ans;
labelrunagain:
cout << "Would you like to run the program again? (Y/N) ";
cin >> ans;
if (ans == 'y' or ans == 'Y')
{
goto labelrunagain;
}
something like that
remember always use goto when u got a problem like this.. saves hella alot of time and really efficient because it is how CPU works.. (jmp instruction)
remember always use goto when u got a problem like this.. saves hella alot of time and really efficient because it is how CPU works.. (jmp instruction)
(I wanted to emphasize the "always" just in case someone thinks I'm overreacting.)
Congratulations. You've just undone thirty years of structured programming. I hope you're happy of what you've accomplished.
Bluejayswhs: Disregard that. Only use goto to, for example, break nested loops. No, for this kind of thing, use a while loop. It would look something like this:
1 2 3
while (ans=='y' || ans=='Y'){
//...
}
A do while, by the way, would check the condition at the end of the loop (which means the loop executes at least once):
By "Fast" you probably mean being able to take the easy way out and type two lines to maximize laziness.
Because "Fast" should mean more efficient, a faster processing script. Using a loop is much more efficient and organized, the only time I ever use goto PlaceInCode; is when I'm just screwing around with something or testing something out.
So to sum up, don't use goto. Use either a DoWhile loop or a While loop.
"Hurr durr. I sacrifice structure for 2 extra nanoseconds."
Seriously, though. There's no excuse. It's not a matter of professionalism or looks. It's a matter of best practices. There's a very good reason why we abandoned goto for general flow, and it can summed up in two words: spaghetti code.
Using goto indiscriminately makes code unreadable, and therefore impossible to debug.
But hey, if you want to use goto in your code, be my guest. Don't tell me I didn't warn you when it's time to debug that code your wrote six months ago and find that you no longer have any idea what anything does because there's a jump to five screens up and seven more jumps inside that. But please don't suggest to the newbies using goto as a replacement for structured flow because it's like bu****e: it's all fun and games until someone loses an eye. What I'm trying to say is that it tends to create bad habits.