使用C中的scanf()从stdin读取输入

假设输入是:

6 4 0 1 2 2 1 0 1 0 0 0 0 1 1 0 0 0 0 1 0 1 1 1 1 0 

图6和4分别是宽度和高度。 我用的是:

 scanf("%d %d", &width, &height); 

然后我使用for循环将其余输入放在2D数组(board [height] [width])中。 问题是关于validation宽度和高度。

当我输入

 6 4 0 1 2 2 1 0 1 0 0 0 0 1 1 0 0 0 0 0 1 1 1 1 0 

错过了一个输入,但它等待直到再输入一个数字。 如果输入的输入不足或太多,我该如何管理并使其发生错误?

有人可以帮我解决这个问题吗? 谢谢!!

你可以使用getsstrtokatoi来完成它。

以下是完整的工作代码。 看到这里工作:

 #include  #include  int main(void) { int w, h, i, j; char buffer[100]; scanf("%d %d", &w, &h); int num[w][h]; gets(buffer); for(i=0; i