Player is not changing between 'X' and 'O' in the program
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.
First I created a 8x5 board.
Second I am prompting the user to select the columns from 1 to 6.
When the user selects any one of columns, then the last row of that column will change from '.'
to 'X'
or 'O'
.
'.'
'X'
'O'
Everything is working fine but the player is not shifting between 'X'
and 'O'
in void TogglePlayer(&player)
'X'
'O'
void TogglePlayer(&player)
#include <iostream>
#include <iomanip>
using namespace std;
const int rows = 8;
const int columns = 5;
char player = 'X';
//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'
void togglePlayer(char &player)
if (player == 'O')
player = 'X';
else player = 'O';
int main()
while (true)
display();
input(player);
togglePlayer(player);
system("pause");
return 0;
for (int i = 7; i >= 0; i++)
@SamVarshavchik When I put a breakpoint on that
for (int i = 7; i >= 0; i++)
and executed my program is running fine but it is not going into the function TogglePlayer
why?– philip
Aug 23 at 0:56
for (int i = 7; i >= 0; i++)
TogglePlayer
Your
togglePlayer
function looks fine to me, btw. Consider moving your char player
declaration into main (just copy/paste that line) so that you have one less effectively global variable, and so that there's no confusion about global char player
vs. togglePlayer's locally-scoped char &player
– zzxyz
Aug 23 at 0:57
togglePlayer
char player
char player
char &player
It takes a very long time to
++
a variable from 7 until it reaches 0. Especially if you debug your code one line at a time.– Sam Varshavchik
Aug 23 at 0:57
++
1 Answer
1
As pointed out in the comments section of the question, the problem is with the for
loop:
for
for (int i = 7; i >= 0; i++)
As you are comparing the value of i
with 0
in the controlling expression, the value of i
should decrease with every iteration.
i
0
i
Here is something that will help you avoid bugs in loops in the future.
Read this question and its answers, especially this one.
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
– weBBer
Aug 23 at 6:24
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
– yakobom
Aug 23 at 6:25
The answer was already given in the comments and the OP found it. I did not want to repeat it again here. I wrote this information as an answer because most people don't read all the comments. But if you insist I can add the answer too.
– P.W
Aug 23 at 6:30
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Keep staring at the following line:
for (int i = 7; i >= 0; i++)
, and keep staring it until you see your bug. After you see it, run a Google search for "undefined behavior".– Sam Varshavchik
Aug 23 at 0:49