Reputation: 11
Total newbie here, having problems searching a 2 dimensional array. I have a 3x3 char array that holds '1' thru '9' like a tic tac toe board. For testing, I hard coded it to search for a '5', hoping it would return '1' for the row. It returns '3' no matter what. There are other posts similar, but they are all too advanced for what minuscule amount I know about c++. Here is my array:
char board[3][3] =
{
{ '1', '2', '3', }, // row 0
{ '4', '5', '6', }, // row 1
{ '7', '8', '9' } // row 2
};
And here's my function:
int searchBoard()
{
char board[3][3];
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
if (board[r][c] == '5')
{
return r;
}
}
}
}
I would really appreciate some help!
Upvotes: 1
Views: 8952
Reputation: 1707
Try changing your function searchBoard
to
int searchBoard()
{
char board[3][3] =
{
{ '1', '2', '3', }, // row 0
{ '4', '5', '6', }, // row 1
{ '7', '8', '9' } // row 2
};
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
if (board[r][c] == '5')
{
return r;
}
}
}
}
Upvotes: 1
Reputation: 23058
You should not declare a local, uninitialized board
in searchBoard()
which masked the global board
.
Upvotes: 1