So I just started using the Goto command and I have some trouble with it. I will post my code and explain the problem.

                cout<<"So here we are at your first choice."<<endl;
                cout<<"-Do you 'stand' there and TRY to get me out of your head..."<<endl;
                cout<<"Or do you 'follow' the strange noises coming from the door?"<<endl;
                string sof;
                firsttp:
                cout<<"-";
                getline (cin, sof);
                if ((sof == "stand")||(sof=="Stand")||(sof=="follow")||(sof=="Follow"))
                {
                    if ((sof=="follow")||(sof=="Follow"))
                    {
                        cout<<"*You get up and go to the door*"<<endl;
                        cout<<"-Well thats it for now xD"<<endl;

                    }
                    else
                        cout<<"*5 hours later*"<<endl;
                        cout<<"-So, not getting anywhere, huh?"<<endl;
                        cout<<"Let me just warp you back a few hour so you can make the 'right' choice."<<endl;
                        cout<<"*You feel a strange buzz*"<<endl;
                        cout<<"So here we are at your first choice."<<endl;
                        cout<<"-Do you 'stand' there and TRY to get me out of your head..."<<endl;
                        cout<<"Or do you 'follow' the strange noises coming from the door?"<<endl;
                        goto firsttp;

                }
                else
                    cout<<"-Well I may be able to talk to you freely but your connection to me is weak."<<endl;
                    cout<<"I can only get 'certain' words... if you know what I mean."<<endl;
                    cout<<"Could you please repeat your choice?"<<endl;
                    goto firsttp;

So the bigger bracket goto works fine and sends me in the beggining where i want it the second one does as well if the conditions (follow,Follow) are not met. But when I type in follow or Follow it gives me the text that I intend ( You get up and go to the door) but it still gets to the first goto it finds and sends me back to the beggining. How do I make it so that only when the Else command is triggered goto is triggered aswell and not to trigger it always ?

Recommended Answers

All 7 Replies

So I just started using the Goto command

Perfect time to stop.

But when I type in follow or Follow it gives me the text that I intend ( You get up and go to the door) but it still gets to the first goto it finds and sends me back to the beggining

That's because the goto isn't part of the else-block. It's outside of the if-else and thus gets executed either way (same as the print statement before it other than the first one).

To clarify this, here's what your code looks like indented correctly:

if ((sof=="follow")||(sof=="Follow"))
{
    cout<<"*You get up and go to the door*"<<endl;
    cout<<"-Well thats it for now xD"<<endl;
}
else
    cout<<"*5 hours later*"<<endl;

cout<<"-So, not getting anywhere, huh?"<<endl;
cout<<"Let me just warp you back a few hour so you can make the 'right' choice."<<endl;
cout<<"*You feel a strange buzz*"<<endl;
cout<<"So here we are at your first choice."<<endl;
cout<<"-Do you 'stand' there and TRY to get me out of your head..."<<endl;
cout<<"Or do you 'follow' the strange noises coming from the door?"<<endl;
goto firsttp;

As you see, everything other than "5 hours later" happens regardless of whether the if- or the else-block was hit. To add the other statements to the else-block, you'll need to add braces around them.

And stop using goto.

commented: Wise advice! +15

You may have come from a language like Python where whitespace and indentation have a particular meaning for scope. In C++ scope is handled a little different; you wrap the bodies of if, else, while, for, and other control structures in { and }. (You did this for both your if statements).

If you do not do that only the first statement will be considered as part of the else clause and evertyhing else will get executed on every pass through the code. You code is as if you had written the following:

if ((sof == "stand")||(sof=="Stand")||(sof=="follow")||(sof=="Follow"))
   {
      if ((sof=="follow")||(sof=="Follow"))
         {
            cout<<"*You get up and go to the door*"<<endl;
            cout<<"-Well thats it for now xD"<<endl;
         } else {
            cout<<"*5 hours later*"<<endl;
         }
         cout<<"-So, not getting anywhere, huh?"<<endl;
         cout<<"Let me just warp you back a few hour so you can make the 'right' choice."<<endl;
         cout<<"*You feel a strange buzz*"<<endl;
         cout<<"So here we are at your first choice."<<endl;
         cout<<"-Do you 'stand' there and TRY to get me out of your head..."<<endl;
         cout<<"Or do you 'follow' the strange noises coming from the door?"<<endl;
         goto firsttp;

when you most likely wanted to use

if ((sof == "stand")||(sof=="Stand")||(sof=="follow")||(sof=="Follow"))
   {
      if ((sof=="follow")||(sof=="Follow"))
         {
            cout<<"*You get up and go to the door*"<<endl;
            cout<<"-Well thats it for now xD"<<endl;
         } else {
            cout<<"*5 hours later*"<<endl;
            cout<<"-So, not getting anywhere, huh?"<<endl;
            cout<<"Let me just warp you back a few hour so you can make the 'right' choice."<<endl;
            cout<<"*You feel a strange buzz*"<<endl;
            cout<<"So here we are at your first choice."<<endl;
            cout<<"-Do you 'stand' there and TRY to get me out of your head..."<<endl;
            cout<<"Or do you 'follow' the strange noises coming from the door?"<<endl;
            goto firsttp;
         }

Ok I get it now , and I havent used any other language before i'm still learning my first language here. And why should I stop using goto, I have read other bad opinions on it , but I need something to send me back in the programm if you could give me another command for that I would be greatful :)

Use a while loop.

I tried but it didnt work properly I wanted the random words entered to keep you in the loop and when you enter some of the right words it sends you out, and still when the character dies or is near death and i need to send him back in time i just use the goto because im not sure how to use a loop there , it will have to be somethin like a loop in a loop in a lop and one loop goes into another loop . Or atleast thats how I picture it in my head :D

Remove all of the goto's and labels. goto should never be used for structuring programs or flow control. It sometimes can be used tactically.

There are a few ways you can use a loop to make this cleaner. Here's one (no testing, be aware of typos):

while (1)
{
    if (answer == "continue")
    {
        cout << "This loop is continuing." << endl;

        continue;    // This stops the current iteration and
                     // moves onto the next iteration.
                     // This doesn't need to be here, since there
                     // is nothing after the if statement anways.
    }
    else
    {
        cout << "This loop is breaking." << endl;

        break;       // This instantly breaks out of the loop.
    }
}

And why should I stop using goto

Because the kind of mind-twisting troubles like the one you encountered now is the only thing that using goto gives you. The idea of the goto is to allow you to jump around to arbitrary places in the code, which might sound like a powerful mechanism, but the only effect it has is to create weird code that is hard to make any sense of, because it jumps all over the place.

On a few occasions, I have encountered code that was full of goto statements, and I really needed to understand the code. The only way that I know of to understand code that is full of gotos is to remove them all (which you can always do), and then you can begin to try to understand it.

There are only a few specific cases where a well-placed goto is beneficial and clearer than the alternatives, but they are rare. In other words, a goto statement should never be your go-to statement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.