Is there any other way to the same thing goto does?(You know what i mean i'm just too lazy too explain it right now >.<)
Goto messes everything up >.
For example, let's say i made a game that displays a menu, like:

"1. Play where is waldo
2.Quit"

Then you choose to play "Where is Waldo".
When you find him the program says "You won! and transports you back to the menu using goto.
But the next time you try to play "Where is Waldo" it'll say "You won!" again.
How can i fix that?
I'd like really simple answers, cuz kinda new to C++ :S
And please make an example too.

Dani AI

Generated

A likely cause of the repeated "You won!" is that the game state (flags, counters, etc.) is not reset when control returns to the menu. Using a jump (goto) can easily skip the code that sets those values back to their starting states, or keep variables in a scope where their values persist. A cleaner pattern is to isolate each play session so its state is freshly created and initialized every time.

One straightforward design: put the menu loop in the main control flow and move each game into its own function. Have a small state object that is explicitly reset at the start of the function. This guarantees a fresh start for each play-through and avoids the fragility of jumps.

struct GameState {
  int lives;
  bool found;
  int score;
};

void resetGame(GameState &s) {
  s.lives = 3;
  s.found = false;
  s.score = 0;
}

void playWhereIsWaldo() {
  GameState state;
  resetGame(state);
  // run the game's loop here, set state.found when found
}

Troubleshooting checklist: 1) Search for globals or static variables that keep values between runs and either reset or remove them. 2) Ensure initialization code runs before each play. 3) Watch for accidental assignment in conditions (use if (flag) or if (flag == true) carefully). 4) Compile with warnings enabled (for example, g++ -Wall) and use simple print/logging or a debugger to confirm variables are reset when returning to the menu.

This follows the intent of (reinitialize state) and the boolean advice from , applied so each play-through starts clean for .

Recommended Answers

All 9 Replies

My guess is that the program needs to reinitialize some variables. But you can almost always avoid using the goto statement at all

bool done = false;
int response;

while( !done )
{
     response = menu();
     if( response == 2
     {
         done = true;
     }
     else
     {
          // initialize game variables here and
          // play game
     }
}

Oh yeah!
Can someone just give me a quick explanation of what bool does?
I kinda understand the conept of it but i don't know how to use it :S
And i didn't quite understand your explanation :S

bool is a variable that only contains 0 (false) or 1 (true). You use it only when you want something to be either true or false. I could also have written the while statement like this: while( done == false)

commented: Thanks +1

bool is the boolean datatype. A variable declared as a bool can have 2 values true (= 1) or false (= 0).

Here is an explanation with an example.

bool is a variable that only contains 0 (false) or 1 (true). You use it only when you want something to be either true or false. I could also have written the while statement like this: while( done == false)

So kinda like:

bool test;
string input;

cout << "hi";
cin >> input;

if(input==hi)
 test = 0

else
 test = 1

if(test = 1)
 cout << "poop";

else
 cout << "wow";

Is that the main idea of how it works?

not ... exaaactly !!

bool is a datatype .. so you cannot do bool == smthing

I don't know what you're trying to do, but here are some modifications.

bool test;
string input;

cout << "hi";
cin >> input;

if(input==hi)
 test = true;
else
 test = false; 

if(test == true)
 cout << "poop";
else
 cout << "wow";

not ... exaaactly !!

bool is a datatype .. so you cannot do bool == smthing

I don't know what you're trying to do, but here are some modifications.

bool test;
string input;

cout << "hi";
cin >> input;

if(input==hi)
 test = true;
else
 test = false; 

if(test == true)
 cout << "poop";
else
 cout << "wow";

sorry, my bad :P

bool test;
string input;

cout << "hi";
cin >> input;

if(input=="hi")
 test = false;

else
 test = true;

if(test == true)
 cout << "poop\n";
else
 cout << "wow\n";

Alright thanks :D

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.