compile error

I am having a error that I can't figure out.The following error occurs in the IF conditional statement.

invalid types `char[int]' for array subscript

for (int i = 0; i < 3; i++)
{
if (theBoard[i][0] == theBoard[i][1] && theBoard[i][1] == theBoard[i][2]) {
bGameOver = true;
bWinGame = true;
}
return;
}
Can we see the definition/declaration of theBoard?
Looks as if you declared a 1D array but you are subscripting it as if it was 2D
I included Main and the function is below. Is there a better way to post this stuff?

#include <iostream> //cin, cout functions

using namespace std;
//Function prototypes
void displayBoard(char[][3]);
void playerInput(int,char[][3],char&);
void checkForWinner(char[][3],bool&,bool&);
void displayWinner(bool, bool, int);

int main()
{
//declare
char caBoard[3][3] ={'1','2','3','4','5','6','7','8','9'};
;
char cPlayerMark; // Stores current player mark

int iPlayerTurn = 1, iCount = 0,
iSet = 1;

bool bGameOver = false, bWinGame = false;

// Main Game While Loop
while(!bGameOver && iCount < 9)
{
displayBoard(caBoard);

// Set player marker: player 1 uses X and player 2 uses O
if (iPlayerTurn == 1)
cPlayerMark = 'X';
else
cPlayerMark = 'O';

playerInput(iPlayerTurn,caBoard,cPlayerMark);

// checkForWinner(caBoard,bGameOver,bWinGame);

// Display Winner
if (bGameOver) {
displayBoard(caBoard);
displayWinner(bGameOver, bWinGame, iPlayerTurn);
}

// Alternative players turns
if (iPlayerTurn == 1)
iPlayerTurn = 2;
else
iPlayerTurn = 1;
// increment to keep under 9 turns
iCount++;
}// End Main While Loop

// Halts program at end
cout << "\n\nPress ENTER to quit.";
cin.get();

return 0;
}
// check for end of the game conditions
void checkForWinner(char theBoard, bool &bGameOver, bool &bWinGame)
{
//check the rows
for (int i = 0; i < 3; i++)
{
if (theBoard[i][0] == theBoard[i][1] && theBoard[i][1] == theBoard[i][2])
{
bGameOver = true;
bWinGame = true;
}
return;
}
//check the clmns
for (int i = 0; i < 3; i++)
{
if (theBoard[0][i] == theBoard[1][i] && theBoard[1][i] == theBoard[2][i])
{
bGameOver = true;
bWinGame = true;
}
return;
}
//check the diagonals
if (theBoard[0][0] == theBoard[1][1] && theBoard[1][1] == theBoard[2][2])
{
bGameOver = true;
bWinGame = true;
}
if (theBoard[2][0] == theBoard[1][1] && theBoard[1][1] == theBoard[0][2])
{
bGameOver = true;
bWinGame = true;
}
//need to check the board full. (No-win conditions).
if (theBoard[0][0] != '1' && theBoard[0][1] != '2' && theBoard[0][3] != '3' &&
theBoard[1][0] != '4' && theBoard[1][1] != '5' && theBoard[1][2] != '6' &&
theBoard[2][0] != '7' &&theBoard[2][1] != '8' && theBoard[2][2] != '9' && !bGameOver)
bGameOver = true;

return;
}//End Function

Your function signature is wrong:
1
2
3
4
5
// Wrong, declaring theBoard as char but using it as char 2D array
void checkForWinner(char theBoard, bool &bGameOver, bool &bWinGame)

// Correct, declaring theBoard as char 2D array
void checkForWinner(char theBoard[3][3], bool &bGameOver, bool &bWinGame)
Last edited on
Thanks, I should've seen that! That's how it goes though.
void checkForWinner(char theBoard[3][3], bool &bGameOver, bool &bWinGame)


I just note C++ compiler is smart enough to figure out the first dimension I presume using some pointer arithmetic.

void checkForWinner(char theBoard[][3], bool &bGameOver, bool &bWinGame)

This explain you can declare like e.g below

void test(char *);

But in your calling code, you can call as e.g below

char p[2] = {'0','1'};
test(p);

or

char *p2 = new char[2];
*p2 = '0'; *(p2+1) = '1';
test(p2);

sohguanh wrote:
C++ compiler is smart enough to figure out the first dimension I presume using some pointer arithmetic.

Actually, even though the first dimension is not required, it is not because the compiler is smart enough to work it out. It can't. Its because the compiler doesn't care about what it is. The other dimensions are required so that the compiler can do the correct pointer arithmetic to locate an element, but the first dimension is not required for that calculation.

I tend to declare all dimensions if I know that they are fixed. What is even better, if the dimensions are fixed, is to pass the array by reference. This forces you to declare all the dimensions and gives a full dimension check at compile time:
 
void checkForWinner(char (&theBoard)[3][3], bool &bGameOver, bool &bWinGame)
Topic archived. No new replies allowed.