I'm currently writing code for a soon to be simple tic tac toe program. Currently i'm just testing out methods of how to run it, so it'll be more "modular" in the end but for now I'm just testing things out. My question is, how would I go about validating that part a 2d array(or in this case a tic tac toe board) has already been given a value. This is my testing code and it stops when I try to validate with a if statement.

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

const gRow = 3;
const gCol = 3;

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

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

do {
showGrid(grid, gRow);

cout << "Player 1, Enter the row and column:";
cin >> rows >> cols;

[B]if ( grid[rows][cols] = 'O')
{ "This space has been taken, enter another row and column";
cin >> rows >> cols; }[/B]
else
grid[rows][cols] = 'X';
showGrid(grid, gRow);
cout << "Player 2, Enter the row and column:";
cin >> rows >> cols;
grid[rows][cols] = 'O';  
}
while(el = 1);
showGrid(grid, gRow);

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;
    }
}

What's bolded is what I thought would work, but doesn't.

Yeah, my bad, I marked as solved so it'll drop.
and to note

if ( grid[rows][cols] = 'O')
{ cout << "This space has been taken, enter another row and column";
cin >> rows >> cols; }

is what the bold was suppsed to show, I didn't type in the cout <<

This article has been dead for over six months. Start a new discussion instead.