Syntax error C

Asked

Viewed 262 times

0

I read the chapter of matrices in Herbert Schildt’s book, C complete and total, and I decided to transcribe the algorithm designed to simulate a game of old. In spite of everything, Dev-c++ found a syntactic error. Someone could answer me why?

/* Um exemplo de jogo-da-velha simples */
#include <stdio.h>
#include <stdlib.h>

char matrix[3][3]; /* A matriz do jogo */

char check(void);
void int_matrix(void);
void get_player_move(void);
void get_computer_move(void);
void disp_matrix(void);

void main(void)
{
char done;

printf("Este é o jogo-da-velha\n");
printf("Você estará jogando contra o computador\n");

done = ' ';
init_matrix();
do{
    disp_matrix();
    get_player_move();
    done = check(); /* Verifica se há vencedor */
    if(done!=' ') break; /* Vencedor */
    get_computer_move();
    done = check(); /* Verifica se há vencedor */
} while(done == ' ');
if(done == 'X') printf("Voce ganhou!\n");
else printf("Eu ganhei!\n");
disp_matrix(); /* Mostra as posições finais */
}

/* Inicializa a matriz */

void init_matrix(void)
{
int i, j;

for(i=0; i<3; i++)
    for(j=0; j<3; j++) matrix[i][j] = ' ';
}

/* Obtém a sua jogada */

void get_player_move(void)
{
int x, y;

printf("Digite as coordenadas para o X: ");
scanf("%d%d", &x, &y);

x--; y--;

if(matrix[x][y] != ' '){
    printf("Posicao invalida, tente novamente\n");
    get_player_move();
}
else matrix[x][y] = 'X';
}

/* Obtém uma jogada do computador */

void get_computer_move(void)
{
int i, j;
for(i=0; i<3; i++){
    for(j=0; j<3; j++)
        if(matrix[i][j] == ' ') break;
    if(matrix[i][j] == ' ') break;
}
if(i*j == 9){
    printf("Empate\n");
    exit(0);
}
else
    matrix[i][j] = 'O';
}

/* Mostra a matriz na tela */

void disp_matrix(void)
{
int t;

for(t=0; t<3; t++){
    printf(" %c | %c | %c ", matrix[t][0], matrix [t][1], matrix[t][2]);
    if(t != 2) printf("\n---|---|---\n");
}
printf("\n");
}

/* Verifica se há um vencedor */

char check(void)
{
int i;

for(i=0; i<3; i++) /* Verifica as linhas */
    if(matrix[i][0] == matrix[i][1] && matrix[i][0] == matriz[i][2]) return matrix[i][0];

for(i=0; i<3; i++) /* Verifica as colunas */
    if(matrix[0][i] == matrix[1][i] && matrix[0][i] == matrix[2][i]) return matrix[0][i];

/* Testa as diagonais */

if(matrix[0][0] == matrix[1][1] && matrix[1][1] == matriz[2][2]) return matrix[0][0];
if(matrix[0][2] == matrix[1][1] && matrix[1][1] == matriz[2][0]) return matrix[0][2];

return ' ';
}

O erro ocorre nesta linha

Error occurs on line 101, as you can see above in the image.

  • First, this book is a bomb. It’s full of mistakes and teaches a lot of wrong. Second, Dev-C++ is another bomb. Get a real IDE. Third, post the bug. Using a decent compiler has many more errors: http://ideone.com/svdKIA

  • The image above contains the error.

  • Just say the line.

1 answer

3


Its headquarters has been declared as Matrix; but not only on the line 101 as well as in 108 and 109 we see calling matrix, therefore the mistake

if(matrix[i][0] == matrix[i][1] && matrix[i][0] == **matriz**[i][2]) return matrix[i][0]; 

replace with:

if(matrix[i][0] == matrix[i][1] && matrix[i][0] == **matrix**[i][2]) return matrix[i][0];

Ready! Now it should work!

Att.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.