Saying that the inputs(stdin) are:
6 4
0 1 1 1 1 0
1 0 0 0 0 1
1 0 0 0 0 1
0 1 1 1 1 0
In the first line, 6 and 4 are width and height respectively.
I did
fgets(buf, sizeof(buf), stdin);
if(strlen(buf) > 4 || strlen(buf) < 4)
{
printf("Cannot decode\n");
return 1;
}
So that if I put the number of integers that is greater than or less than 2 as for the first line(width and height), then press enter, it occurs error.
Next step is to put the rest of the inputs in 2D array, board[height][width].
What I did is:
for(i = 0; i < height; i++)
{
for(j = 0; j < width; j++)
{
scanf("%d", &input);
board[i][j] = input;
}
}
But the problem is, the matrix needs to match the width and height.
For example, if I enter 6 and 4 as its width and height respectively, and then if I put
0 1 1 1 1
then press enter, it needs to occur an error immediately as the number of integers and the width do not match.
scanf ignores the enter key... so how can I make the program occur an error if I put a number of integers that do not match width and height?