I'm learning C from a book and they have this sample code:
Code:
I copied it exactly the way it was in the book, I double checked it, I saved it with the extension .c and the compiler doesn't give me any error, yet when I run it the console displays a bunch of gibberish and another window pops up saying that the cmd crashed. What could be causing this?
Every program in the book so far worked just fine, maybe it's my computer?
Code:
#include <stdio.h>
#define DIM 3
#define SPACE ' '
void inic(char s[ ][DIM])
{
int i,j;
for(i=0; i<DIM; i++)
for (j=0; j<DIM; j++)
s[i][j] = SPACE;
}
void show (char s[DIM][DIM])
{
int i, j;
for (i=0; i<DIM; i++)
{
for ( j=0; i<DIM; j++)
printf("%c %c", s[i][j], j==DIM-1?' ':'|');
if (i!=DIM-1)
printf ("\n--------");
putchar('\n');
}
}
main()
{
char TicTacToe[DIM][DIM];
int posx, posy;
char ch = '0';
int n_plays = 0;
inic(TicTacToe);
while (1)
{
show(TicTacToe);
printf("\nInput the coordinates of your move row column: ");
scanf("%d %d", &posx, &posy);
posx--; posy--;
if (TicTacToe[posx][posy] == SPACE)
{ TicTacToe[posx][posy] = ch = (ch== '0') ? 'X' : '0';
n_plays++;
}
else
printf("Already filled position\n Try again!!\n");
if (n_plays==DIM*DIM)
break;
}
show(TicTacToe);
}
I copied it exactly the way it was in the book, I double checked it, I saved it with the extension .c and the compiler doesn't give me any error, yet when I run it the console displays a bunch of gibberish and another window pops up saying that the cmd crashed. What could be causing this?
Every program in the book so far worked just fine, maybe it's my computer?