Run Program again loop.

Sep 13, 2008 at 5:17am
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;

if (ans == 'y' or ans == 'Y')
{
run again
}

else
return 0;


Can you folks help me please?
Sep 13, 2008 at 5:33am
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)


Here's a working example from microsoft
1
2
3
4
5
6
7
8
9
10
11
12
// labels_with_goto.cpp
// compile with: /EHsc
#include <iostream>
int main() {
   using namespace std;
   goto Test2;

   cout << "testing" << endl;

   Test2:
      cerr << "At Test2 label." << endl;
}
Last edited on Sep 13, 2008 at 5:34am
Sep 13, 2008 at 5:42am
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):

1
2
3
do {
    //...
} while (ans=='y' || ans=='Y');
Last edited on Sep 13, 2008 at 5:43am
Sep 13, 2008 at 6:00am
what goes inside of the {} to make the program jump back up to top?
Sep 13, 2008 at 6:05am
Whatever you need to run more than once. The std::cin should definitely be inside. And declare ans before the loop to avoid definition errors.
Sep 13, 2008 at 7:04am
Lol.. yes i was kidding but using goto's is a quick way to get things done (learned that from visual baisc ;p).

sure a while loop would look more (professional here)

but then again do you want to impress someone ? or do you just want to make it work?..

For me.. i dont care how code looks I just want it to be fast.
Sep 13, 2008 at 7:19am
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.
Sep 13, 2008 at 7:24am
"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.
Topic archived. No new replies allowed.