0

I'm running into an error and I do not know how to fix it.

The essence of this function is to take a user's coordinate and input the player's symbol into the chosen coordinate. I did this by taking the input as a char, using strcmp to compare it to one of the 9 coordinate spaces, and then converting it into an integer using if statements. It would then input the player's symbol into this space. However, I wanted to make sure that the user is not inputting invalid coordinates. The problem is it keeps saying the input is invalid. I tried debugging using "A1". I checked strcmp and it resulted in 0. I checked invalid and it resulted in 0. I then checked row_move and column_move and they're getting assigned to the correct value of [0][2]. However, I did notice initially when I input A1, the column_move value is extremely high arbitrary number. Can someone help me figure this out?

void Player1Move()
{       
        char p1move[2];
        int row_move, column_move, invalid;
        row_move = 0; 
        column_move = 0;
        invalid = 0; 
        printf("It's Player 1's turn!\n");
        printf("What's your move?\n");
        int rescheck;
        do
        {       
                printf("Enter your move ");
                scanf("%s", p1move); 
                printf("YOUR MOVE IS %s\n", p1move);
                printf("Check Invalid: %d\n", invalid);
                rescheck = strcmp(p1move,"A1"); 
                printf("Check String A1: %d\n", rescheck);
                printf("Row: %d\n", row_move);
                printf("Column: %d\n", column_move);
                if(strcmp(p1move,"A1") == 0)
                {       
                        row_move = 2; 
                        column_move = 0; 
                } else if(strcmp(p1move, "A2") == 0)
                {       
                        row_move = 1; 
                        column_move = 0; 
                } else if(strcmp(p1move, "A3") == 0)
                {       
                        row_move = 0; 
                        column_move = 0; 
                } else if(strcmp(p1move, "B1") == 0)
                {       
                        row_move = 2; 
                        column_move = 1; 
                } else if(strcmp(p1move, "B2") == 0)
                {       
                        row_move = 1; 
                        column_move = 1; 
                } else if(strcmp(p1move, "B3") == 0)
                {       
                        row_move = 0; 
                        column_move = 1; 
                } else if(strcmp(p1move, "C1") == 0)
                {       
                        row_move = 2; 
                        column_move = 2; 
                } else if(strcmp(p1move, "C2") == 0)
                {       
                        row_move = 1; 
                        column_move = 2; 
                } else if(strcmp(p1move, "C3") == 0)
                {       
                        row_move = 1; 
                        column_move = 2;
                } else
                {       
                        invalid = 1;
                }
                
                /* check if board space is available */
                
                if(invalid == 1 || board[row_move][column_move] != ' ')
                {       
                        printf("That is not a valid space, please pick a new space\n");
                } else
                {       
                        board[row_move][column_move] = PLAYER1;
                        break;
                }
        }       while(board[row_move][column_move] != ' ');
}

Edit 2:

I edited the function and still producing the same error.

It's Player 1's turn!
What's your move?
Enter your move A1
Move is [0][4196901]
Check Invalid: 0
String: A1
That is not a valid space, please pick a new space
Enter your move 

This is what happens when I initially put in A1, I'm still getting this error. I had changed the char size and updated the scanf. I had checked to see if anything is being affected such as the row and column choices along with the invalid check. As shown here, there seems to be an error with column_move and it's still an arbitrary number.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373

1 Answers1

2

You declared a character array with two elements

char p1move[2];

So you may enter a string that contains only one character apart from the terminating zero character '\0'.

However you are trying to compare the content of the array with strings that contain three elements (including the terminating zero character '\0') as for example

if(strcmp(p1move,"A1") == 0)

Note: the string literal "A1" is stored as a character array with three characters { 'A', '1', '\0' }.

If you will try to enter two characters then the behavior will be undefined because you will overwrite memory beyond the array.

Declare the array as having at least three elements

char p1move[3];

and rewrite the call of scanf like

scanf("%2s", p1move);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • As long as the input into `p1move` is just a single character, then the comparisons are fine (but will fail). If the input to `p1move` is more than one character (e.g. `A1`), then the buffer overflow from `scanf()` leads to undefined behaviour. The problem isn't with `strcmp()` — the problem is (as ever) with the abuse of `scanf()` and not allocating anywhere near enough space to handle even valid input, let alone invalid input. – Jonathan Leffler Jul 06 '22 at 23:12