The following code with line 18 commented, given this data:
1
3 3 1
yields
field 1 1
field 1 2
field 1 3
field 2 1
field 2 2
field 2 3
field 3 1
field 3 2
field 3 3
However if I leave the line as it is, it only executes the scanf(…) every second step. Where is the problem?
#include <stdio.h>
char field[102][102];
main()
{
int t;
scanf("%d",&t);
while (t--)
{
int r, c, n, i, j;
scanf("%d %d %d", &r, &c, &n); // wczytanie liczby wierszy, kolumn, dni
for (i=1; i<=r; i++) // wiersze
{
for (j=1; j<=c; j++) // kolumny
{
printf("field %d %d\n", i, j);
scanf("%c", &field[i][j]); // line 18 here
}
}
}
return 0;
}
The problem is that when you scan for a character you actually send two: the letter, which gets scanf-ed, and and “hidden” ‘\n’ which is read the next time. Just use a getch() after the scanf to read (and discard) that newline.