A problem with goto

Hello. I am having problems using goto. What I would like to do is break out of all of the nested loops a once when playerPlacement[k] == 50. This seems like it should work, but it's not. The rest of my code seems to be working as desired. Do any of you know what is wrong? goto is on line 42.

I should clarify: when a player reaches space 50 the loops continue to execute almost as if the goto is being ignored.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  int main()
{
	srand(time(NULL));
	int numPlayers;
	int playerPlacement[4] = {0};
	int firstRoll, secondRoll, bothRolls;
	int m;
	
	gameInfo(); // calling gameInfo outputs the rules of the game

	cout << "Input the number of players: ";
	cin >> numPlayers;
	cout << endl;

	while (numPlayers != 2 && numPlayers != 3 && numPlayers != 4) // input validation. No more than four players can play.
	{
		cout << "Only four players can play this game at a time. Please enter 2, 3, or 4 for the number of players: ";
		cin >> numPlayers;
		cout << endl;
	}

	while (playerPlacement[0] != 50 && playerPlacement[1] != 50 && playerPlacement[2] != 50 && playerPlacement[3] != 50)
	{
		for (int k = 0; k < numPlayers; k++)
		{
			diceRoll(k, firstRoll, secondRoll, bothRolls); // the player's rolls are pseudo-randomly generated
			playerLocation(k, firstRoll, secondRoll, bothRolls, numPlayers); // the player's movements and placement are determined

			if (firstRoll == secondRoll) // if player rolls doubles they get to roll again
			{
				cout << "Player " << k + 1 << " gets to roll again.\n" << endl;
				system("pause"); // just for user interaction

				diceRoll(k, firstRoll, secondRoll, bothRolls); // the player's rolls are pseudo-randomly generated
				playerLocation(k, firstRoll, secondRoll, bothRolls, numPlayers); // the player's movements are placement are determined
			}

			board(k, numPlayers); // a board showing the player's placement is outputted
			cout << endl;
			system("pause"); // just for user interaction

			if (playerPlacement[k] == 50)
			{
				m = k;
				goto FINISHED;
			}
			
			if (k == numPlayers - 1) // if a round has cycled through (each player has gone once), a round wrap-up is outputted showing the placements of each player on the board
			{
				cout << endl << "ROUND WRAPUP:\n" << endl;

				for (k = 0; k < numPlayers; k++)
				{
					cout << "Player " << k + 1 << ".\n";
					board(k, numPlayers);
				}

				cout << endl;
				system("Pause"); // just for user interaction
			}
		}
	}
			
	FINISHED: cout << endl << "Player " << m + 1 << " reached space 50! They are the WINNER!\nWould you like to play again?\n" << endl;

	system("pause");
	return 0;
}
Last edited on
You haven't given all the code, so we can't actually run it.
But my guess is that playerPlacement[k] doesn't equal 50, i.e., the if body isn't executed.
Add a print statement (like: cerr<<"goto FINISHED\n";) before the goto to see if it's executed.
Last edited on
Hey, thanks for replying. I substituted a few for loops instead of goto, but i am still having the same problem. My entire program is pretty long, here is part 1:

#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

int playerPlacement[4];

void gameInfo() //this function contains the rules for the game, which to user sees
{
cout << "This is a pseudo-Sorry game where the players continue rolling the dice until one of them reaches space 50.\n" << endl;
cout << "The rules are:\nIf a player roles a 4 they move back four spaces.\nIf a player roles a 7 they swap places with the leading player.\n";
cout << "If a player roles a 9 they move back one space.\nIf a player roles an 11 they swap places with the person in last.\n";
cout << "If a player roles a 12 they return to start.\nIf a player roles a value other that those specified above, they advance that many spaces.\n";
cout << "If a player roles doubles they may roll again.\nA player must roll doubles to advance from start.\n" << endl;

}

void diceRoll(int k, int& roll1, int& roll2, int& rollTotal) //function that produces the pseudo-random numbers between 1 and 6.
{
roll1 = rand() % 6 + 1;
roll2 = rand() % 6 + 1;
rollTotal = roll1 + roll2;

cout << "Player " << k + 1 << " rolled a " << roll1 << " and a " << roll2 << " for a total of " << rollTotal << ".\n";
}

void playerLocation(int k, int roll1, int roll2, int rollTotal, int numPlayers) //function that determines the player's location based on the value that they rolled.
{
int leadingPlayer, lastPlayer, placeHolder; // leadingPlayer, lastPlayer, and placeHolder are used to store player locations
int p = 0; // p, m, and q are used to store the number of loop iterations
int q = 0;
int m;

if ((roll1 == roll2 && playerPlacement[k] == 0) || playerPlacement[k] != 0) // the switch statement only executes if a player roles doubles or is not at start
{
switch (rollTotal)
{
case 2: // if a player roles 2 they advance 2 places, unless they are at space 49
if (playerPlacement[k] <= 48)
{
playerPlacement[k] = playerPlacement[k] + 2;
cout << "Player " << k + 1 << " advances two places.\n";
}

break;
case 3: // if a player roles 3 they advance 3 spaces, unless they are at a space greater than 47.
if (playerPlacement[k] <= 47)
{
playerPlacement[k] = playerPlacement[k] + 3;
cout << "Player " << k + 1 << " advances three places.\n";
}
break;
case 4: // if a player roles 4 they move back 4 spaces, unless they are at a space less that 4, in which case they move to start
if (playerPlacement[k] <= 4)
{
playerPlacement[k] = 0;
cout << "Player " << k + 1 << " is at start.\n";
}
else
{
playerPlacement[k] = playerPlacement[k] - 4;
cout << "Player " << k + 1 << " moves back four spaces.\n";
}
break;
case 5: // if a player roles a 5 they advance 5 spaces, unless at a space greater than 45.
if (playerPlacement[k] <= 45)
{
playerPlacement[k] = playerPlacement[k] + 5;
cout << "Player " << k + 1 << " advances five places.\n";
}
break;
case 6: // // if a player roles a 6 they advance 6 spaces, unless at a space greater than 44.
if (playerPlacement[k] <= 44)
{
playerPlacement[k] = playerPlacement[k] + 6;
cout << "Player " << k + 1 << " advances six places.\n";
}
break;
case 7: // if a player roles a 7 they switch places with the leading player. If they are the leading player, they do not move.
if (playerPlacement[k] < playerPlacement[0] || playerPlacement[k] < playerPlacement[1] || playerPlacement[k] < playerPlacement[2] || playerPlacement[k] < playerPlacement[3])
{
leadingPlayer = playerPlacement[k];
placeHolder = playerPlacement[k];
for (int n = 0; n < numPlayers; n++)
{
if (playerPlacement[n] > leadingPlayer)
{
leadingPlayer = playerPlacement[n];
p = n;
}
}
playerPlacement[k] = leadingPlayer;
playerPlacement[p] = placeHolder;
cout << "Player " << k + 1 << " has switched places with player " << p + 1 << ".\n";
}
else
cout << "No player is farther along on the board to switch places with.\n";
break;
case 8: // if a player roles a 8 they advance 8 spaces, unless at a space greater than 42.
if (playerPlacement[k] <= 42)
{
playerPlacement[k] = playerPlacement[k] + 8;
cout << "Player " << k + 1 << " advances eigth places.\n";
}
break;
case 9: // if a player roles a 9 they move back one space.
if (playerPlacement[k] == 0)
{
playerPlacement[k] = 0;
cout << "Player " << k + 1 << " is at start.\n";
}
else
{
playerPlacement[k] = playerPlacement[k] - 1;
cout << "Player " << k + 1 << " moves back 1 space.\n";
}
break;
case 10: // if a player roles a 10 they advance 10 spaces, unless at a space greater than 40.
if (playerPlacement[k] <= 40)
{
playerPlacement[k] = playerPlacement[k] + 10;
cout << "Player " << k + 1 << " advances ten places.\n";
}
break;
case 11: // if a player roles an 11, they switch places with the player in last. If they are in last they do not move.
if (playerPlacement[k] > playerPlacement[0] && playerPlacement[0] != 0 || playerPlacement[k] > playerPlacement[1] && playerPlacement[1] != 0 || playerPlacement[k] > playerPlacement[2] && playerPlacement[2] != 0 || playerPlacement[k] > playerPlacement[3] && playerPlacement[3] != 0)
{
lastPlayer = playerPlacement[k];
placeHolder = playerPlacement[k];
for (int n = 0; n < numPlayers; n++)
{
if (playerPlacement[n] < lastPlayer && playerPlacement[n] != 0)
{
lastPlayer = playerPlacement[n];
q = n;
}
}
playerPlacement[k] = lastPlayer;
playerPlacement[q] = placeHolder;
cout << "Player " << k + 1 << " has switched places with player " << q + 1 << ".\n";
}
else
cout << "No player is farther behind on the board to switch places with.\n";
break;
case 12: // if a player roles 12 they return to start.
playerPlacement[k] = 0;
cout << "Player " << k + 1 << " is at start.\n";
break;

}

for (int m = 0; m < numPlayers; m++) // if a player lands on a spot held by another player, they send that player back to start.
{
if (playerPlacement[k] == playerPlacement[m] && playerPlacement[k] != 0 && m != k)
{
playerPlacement[m] = 0; // if player k equals player m, player m is kicked back to start.
cout << "Player " << m + 1 << " was kicked back to start by player " << k + 1 << ".\n";
}
}
}
else // if a player does not roll doubles, they are at start.
{
playerPlacement[k] = 0;
cout << "Player " << k + 1 << " is at start.\n";
}

}


Here is part 2:

void board(int k, int numPlayers) // this is the function that outputs a board showing where the player's are.
{
cout << char(201); // where the top begins

for (int j = 1; j < 50; j++)
{
cout << char(205);
cout << char(203);
}
cout << char(205);
cout << char(187);
cout << endl; // where the middle begins
cout << char(186);

for (int n = 1; n < 51; n++)
{
if (n == playerPlacement[k])
cout << k + 1;
else
{
cout << ' ';
}
cout << char(186);
}
cout << endl << char(200); // where the bottom begins
for (int m = 1; m < 50; m++)
{
cout << char(205);
cout << char(202);
}
cout << char(205) << char(188);
cout << endl;
}



int main()
{
srand(time(NULL));
int numPlayers;
int playerPlacement[4] = {0};
int firstRoll, secondRoll, bothRolls;
int m;

gameInfo(); // calling gameInfo outputs the rules of the game

cout << "Input the number of players: ";
cin >> numPlayers;
cout << endl;

while (numPlayers != 2 && numPlayers != 3 && numPlayers != 4) // input validation. No more than four players can play.
{
cout << "Only four players can play this game at a time. Please enter 2, 3, or 4 for the number of players: ";
cin >> numPlayers;
cout << endl;
}

while (playerPlacement[0] != 50 && playerPlacement[1] != 50 && playerPlacement[2] != 50 && playerPlacement[3] != 50)
{
for (int k = 0; k < numPlayers; k++)
{
diceRoll(k, firstRoll, secondRoll, bothRolls); // the player's rolls are pseudo-randomly generated
playerLocation(k, firstRoll, secondRoll, bothRolls, numPlayers); // the player's movements and placement are determined

if (playerPlacement[k] == 5)
{
cout << endl << "Player " << k + 1 << " reached space 50! They are the WINNER!\nWould you like to play again?\n" << endl;
}
else
{
if (firstRoll == secondRoll) // if player rolls doubles they get to roll again
{
cout << "Player " << k + 1 << " gets to roll again.\n" << endl;
system("pause"); // just for user interaction

diceRoll(k, firstRoll, secondRoll, bothRolls); // the player's rolls are pseudo-randomly generated
playerLocation(k, firstRoll, secondRoll, bothRolls, numPlayers); // the player's movements are placement are determined
}

if (playerPlacement[k] == 50)
{
cout << endl << "Player " << k + 1 << " reached space 50! They are the WINNER!\nWould you like to play again?\n" << endl;
}
else
{
board(k, numPlayers); // a board showing the player's placement is outputted
cout << endl;
system("pause"); // just for user interaction

if (k == numPlayers - 1) // if a round has cycled through (each player has gone once), a round wrap-up is outputted showing the placements of each player on the board
{
cout << endl << "ROUND WRAPUP:\n" << endl;

for (k = 0; k < numPlayers; k++)
{
cout << "Player " << k + 1 << ".\n";
board(k, numPlayers);
}

cout << endl;
system("Pause"); // just for user interaction
}
}
}
}
}

system("pause");
return 0;
}
You will have to pass that playerplacement array into your function playerLocation before it can work with it.
@Manga, playerPlacement is global. But the code is unreadable without indentation, anyway.
@tpb

No, it is declared after main.

1
2
3
4
5
6
7
 int main()
{
	srand(time(NULL));
	int numPlayers;
	int playerPlacement[4] = {0}; //right here in fact.
	int firstRoll, secondRoll, bothRolls;
	int m;
@Manga, I didn't notice it in main, but it's also declared globally. I guess that may have been the problem. He was changing the global one in the functions but using the local one in main.
Topic archived. No new replies allowed.