Yes, I have the do while set to go on forever until I get on to the solving part, but while trying to shrink my program down so I can make it more modular, the O's that player 2 have are showing up as a face?

I know I have a logic error with the player 1 and 2 alternating, I'll get that fixed, but first I want to know why my O's are showning up as faces?

#include <iostream>
#include <iomanip>
using namespace std;

const gRow = 3;
const gCol = 3;

void showGrid(char grid[][gCol], int);
int changeValue(int, int);

int main()
{ char grid[gRow][gCol] =  {{'*', '*', '*'},
							{'*', '*', '*'}, 
							{'*', '*', '*'}};
	int rows, cols;
	int el = 1;
	int turn = 1;
	int num = 1;
	char player;
	
cout << "         TIC TAC TOE" << endl;
cout << "Player One: X    Player Two: O\n" << endl;

do {
	
showGrid(grid, gRow);

if (turn % 2)
	{player = 'X';}
	else {player = 'O' && num++;}

cout << "Player " << num << ", Enter the row and column:";
cin >> rows >> cols;
if (grid[rows-1][cols-1] != '*')
{cout << "This space is taken... Enter the row and column:\n";
cin >> rows >> cols; }
	
grid[rows - 1][cols - 1] = player;

turn++;
 
}
while(el = 1);


return 0;

}


void showGrid(char array[][gCol], int numRows)
{
    for (int row = 0; row < numRows; row++)
    {   for (int col = 0; col < gCol; col++)
        {
            cout << setw(2) << array[row][col] << " ";
        }
        cout << endl;
    }
}

Dani AI

Generated

Short diagnosis and concrete fixes rooted in the posts by and .

The “happy face” appears because the else branch ends up assigning the boolean result (0 or 1) to the char player. Consoles render those low char codes as glyphs or control symbols, so instead of 'O' a strange symbol shows. correctly called out the operator misuse; ’s notes about types and input checking are also on point.

Correct the player/number logic so the character and player index are set explicitly, for example:

if (turn % 2 == 1) { player = 'X'; num = 1; }
else                 { player = 'O'; num = 2; }

Validate input with a loop that checks bounds and occupancy before writing the grid. This avoids out-of-range indexes and repeated single retries:

while (true) {
  cout << "Player " << num << ", enter row and col: ";
  if (!(cin >> rows >> cols)) break;
  if (rows < 1 || rows > gRow || cols < 1 || cols > gCol) { cout << "Invalid; try again\n"; continue; }
  if (grid[rows-1][cols-1] != '*') { cout << "Taken; try again\n"; continue; }
  grid[rows-1][cols-1] = player;
  break;
}

Other small but important fixes: declare the sizes with an integral type (const int gRow = 3; const int gCol = 3;), and avoid while(el = 1) which assigns instead of compares — use while(el == 1) or while (true) with explicit breaks. Remove or implement unused prototypes (like changeValue) to keep the build clean. These changes together will stop the odd characters and make the turn and input flow predictable and robust.

Recommended Answers

All 2 Replies

Hmm not exactly sure what that would be happening, but there are still a couple things you need to change. At the top you should declare gRow and gCol as const int - I'm not sure how this even compiles with no type given. Also you should make a loop for getting input rather than an if statement in case the player tries to place a piece in an invalid spot more than once. Another thing to consider is making sure the player enters a valid row and column (between 1 and 3).

Sorry but your errors include : else {player = 'O' && num++;} And that is equivilent to

player = ('O' && num);
num++;

That means that player == 1 or 0, and both of those are interesting ascii characters to print :)

hope that helps.

Overall, laying out the code cleanly will greatly reduce errors like this.

commented: Nice catch +24
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.