-1
string command;
int ZaneNightTrain;

NightTrain:

cin.ignore();
getline(cin, command);
transform(command.begin(), command.end(),command.begin(), ::toupper);
if (command == "TALK TO ZANE")
{

    if (ZaneNightTrain == 0)
    {
        cout << "\nZane: Blah Blah Blah 1\n\n";
        ZaneNightTrain++;

        goto NightTrain;
    }
}
else if (ZaneNightTrain == 1)
{
    cout << "\nZane: Blah blah blah 2\n" << endl;
    ZaneNightTrain++;
    goto NightTrain;
}
else if (ZaneNightTrain == 2)
{
    cout << "\nBlah blah blah 3\n" << endl;

    ZaneNightTrain = 0;

    goto NightTrain;
}

return 0;
}

I have no idea why ZaneNightTrain = 0; ends the program automatically. I could set the number to 2 and it does what it's supposed to be doing. I tried setting it up so the first thing he says is a 1 instead of a 0 and it starts ending the program as well. I had another version of this code where you enter a number instead of a string to talk and it has no problems.

Ilya
  • 4,583
  • 4
  • 26
  • 51
Dave
  • 21
  • 2
  • 2
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Dec 02 '16 at 05:22
  • This question is currently unanswerable without guesswork. Suggestion: replace those `goto`s with a loop that has a nice, testable condition, then take a look at what you feed into the condition. Why make things so hard on yourself? – user4581301 Dec 02 '16 at 05:31
  • If `command` _isn't_ "TALK TO ZANE" the return at the end of the function will be executed if `ZaneNightTrain` is anything other than 1 or 2. – 1201ProgramAlarm Dec 02 '16 at 06:06

1 Answers1

2

You're not initializing the value of ZaneNightTrain

int ZaneNightTrain = 0;

This previous question may help you more fully understand what is happening. Why do I see strange values when I print uninitialized variables?

The TLDR is that the value of a declared variable is not guaranteed and you must set it to a default value if you're going to check that as the first operation in your program.

Community
  • 1
  • 1
sclarson
  • 4,362
  • 3
  • 32
  • 44
  • I tried that and it seems to not set it up to a 0. The numbers are still going up it seems. – Dave Dec 02 '16 at 05:25
  • You should either attach a debugger our add some cout statements to look at the values of your variables before your comparisons. They probably are not what you think they are. – sclarson Dec 02 '16 at 05:30