Player is not changing between 'X' and 'O' in the program

I want to create a game (connect four) in c++ using arrays and loops.

1) First I created a 8x5 board.

2) Second I am prompting the user to select the columns from 1 to 6.

3) When the user selects any one of columns, then the last row of that column will change from '.' to 'X' or 'O'.

Everything is working fine but the player is not shifting between 'X' and 'O'


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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  #include <iostream>
#include <iomanip>
using namespace std;

const int rows = 8;
const int columns = 5;

//This function creates a 8x5 board
char matrix[rows][columns] = { '.','.','.','.','.',
								'.','.','.','.','.',
								'.','.','.','.','.',
								'.','.','.','.','.',
								'.','.','.','.','.',
								'.','.','.','.','.',
								'.','.','.','.','.',
								'.','.','.','.','.' };

//This function displays the board
void display()
{
	int width = 3;
	cout <<  setw(width) << "1" << setw(width) << "2" << setw(width) << "3" << setw(width) << "4" << setw(width) << "5" << setw(width) << '\n';
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < columns; j++)
		{
			cout << setw(3) << matrix[i][j] << setw(3);
		}
		cout << endl;
	}
	cout << setw(width) << "1" << setw(width) << "2" << setw(width) << "3" << setw(width) << "4" << setw(width) << "5" << setw(width) << '\n';
}


//This the main function that executes the player's selected column
void input(char player)
{
	int a;
	cout << "Enter the column" << endl;
	cin >> a;
	if (a > 0 && a < 6)
	{
		for (int i = 7; i >= 0; i++)
		{
			if (matrix[i][a - 1] == '.')
			{
				matrix[i][a - 1] = player;
				break;
			}
		}
	}
}



//This function changes the players between 'X' or 'O'
char togglePlayer(char player)
{
	if (player == 'X')
	{
		return player = 'O';
	}
	else
	{
		return player = 'X';
	}
}




int main()
{
	char player = 'X';
	
	while (true)
	{
		display();
		input(player);
		togglePlayer(player);
	}
	system("pause");
	
	return 0;
}
Player is not changing between 'X' and 'O' in the program

You had made a topic about this problem previously.
http://www.cplusplus.com/forum/beginner/241168/

In that thread, FurryGuy and mbozzi said what you needed to fix.

Change your togglePlayer function to:
1
2
3
4
5
6
7
8
9
10
11
void togglePlayer(char& player)
{
	if (player == 'X')
	{
		player = 'O';
	}
	else // player presumably == 'O'
	{
		player = 'X';
	}
}


Passing player by reference lets the function directly modify the actual player instead of a copy of the player.
Last edited on
closed account (E0p9LyTq)
Either pass your player variable into your togglePlayer function by reference, as been previously suggested multiple times.

OR, modify what you do in main():

player = togglePlayer(player);

You modify a locally created copy of player in the function. You don't change the value in main(). When you return the changed copy you do nothing with it.
Topic archived. No new replies allowed.