0

I have a function:

void getInput(int turn)
{
    int posI, posII, i, j;
    char posC, c;
    if(turn == 1)
    {
        c = 'B';
    }
    else if(turn == 2)
    {
        c = 'W';
    }
    int count = 0;
    while(1)
    {
        printf("Enter the Cell:\n");
        count++;
        printf("I'm Here %d", count);

        scanf("%c%d", &posC, &posI);
        posII = (int)posC - (int)'A';
        posI = posI - 1;
        if(board[posII][posI] == '*')
        {
            board[posII][posI] = c;
            for(i = 0 ; i < 8 ; i++)
                for(j = 0 ; j < 8 ; j++)
                {
                    if(board[i][j] == '*')
                        board[i][j] = ' ';
                }
            chageCell(posII, posI, c, 1, 0);
            break;
        }
        else
        {
            printf("Wrong Cell\n try Again: \n");
        }
    }
}

This is a part of Othello game, the problem is when I enter the wrong cell position, the message Wrong cell appears in console three times but logically it is not possible, after each time showing the Wrong cell message it should wait for the second input.

melpomene
  • 84,125
  • 8
  • 85
  • 148
Leo
  • 867
  • 1
  • 15
  • 41

2 Answers2

1

when I enter the wrong cell position, the message "Wrong cell" appears in console three times

Depending on the input, "Wrong cell" will appear as many times as it takes the loop to work through the invalid input one character at a time.

Consider:

scanf("%c%d", &posC, &posI);

When the input is valid, scanf will read a character and a number. When the input is invalid, scanf will read only a character, leaving everything else in the input buffer to be consumed by subsequent calls of scanf.

You can tell if scanf didn't read the int by checking its return value:

if (scanf("%c%d", &posC, &posI) != 2) {
    // The input is incomplete. Ignore the input to end-of-line
    ...
    continue;
}
// If the code reaches this line, you know that both `posC` and `posI` have been read correctly.
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Your scanf isn't failing.

Your logic is incorrect. The board is always '*' at the requested position.

To explain better, I am making the following assumptions about your game: * You initialized an 8 x 8 board, with each cell to '*' at some point.

If you make the following change:

  else
        {
            printf("posC=%c, posI=%d\n", posC, posI);
            printf("Wrong Cell\n try Again: \n");
        }

then you will see that you are obtaining values from the scanf.

I created the following and added it to the code:

void printBoard() {
    int i,j;

    printf("\n");
    for (i=0; i<9; i++) {
        for (j=0; j<9; j++) {
            printf(" %c", board[i][j]);
        }
        printf("\n");
    }
    printf("\n");

}

and then added it as follows:

       else
       {
            printf("posC=%c, posI=%d\n", posC, posI);
            printBoard();
            printf("Wrong Cell\n try Again: \n");
        }

You will see that the board never changes.

Cœur
  • 37,241
  • 25
  • 195
  • 267
jhenderson2099
  • 956
  • 8
  • 17