Search code examples
c++multidimensional-arraycharstrcmpatoi

checking values in a char multidimensional array, cpp


I am trying to check whether a user input matches a specific value in a char multidimensional array. But I cant seem to figure out a way to do so. The user input will be a number from 1-9. I will be looping through my array to find a matching value. My code is as follows,

 char input;
 getline(cin,input);
 char gameBoard[3][3] = {{'1','2','3'},
                         {'4','5','6'},
                         {'7','8','9'}
                        };
for(int i=0; i<3; i++)
{
    for(int j=0; j<3; j++)
    {
        gameBoard[i][j];  //SOME CODE TO COMPARE THE VALUE TO USER INPUT
    }
} 

I have tried using strcmp but can only do it for a single dimensional array.

I have also tried converting the char value into a number by atoi but i get an error,

error: invalid conversion from 'char' to 'const char*' [-fpermissive]|

Any suggestions on how to compare will be appreciated. Thanks


Solution

  • How about just:

    if (gameBoard[i][j] == input) {... }