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.
答案 0 :(得分: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.