Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在C语言的这个嵌套for循环中跳过scanf()?_C_For Loop_Scanf - Fatal编程技术网

为什么在C语言的这个嵌套for循环中跳过scanf()?

为什么在C语言的这个嵌套for循环中跳过scanf()?,c,for-loop,scanf,C,For Loop,Scanf,我正在为类编写一个程序,它要求用户输入一个2D数组大小的数组,然后让用户输入数组的值。这是我目前掌握的代码: #include <stdio.h> int main(void) { // Setup int N, M; int row = 0, col = 0; printf("\n"); printf("This program counts occurences of digits 0 through 9 in an NxM arra

我正在为类编写一个程序,它要求用户输入一个2D数组大小的数组,然后让用户输入数组的值。这是我目前掌握的代码:

#include <stdio.h>

int main(void)
{
    // Setup 
    int N, M;
    int row = 0, col = 0;

    printf("\n");
    printf("This program counts occurences of digits 0 through 9 in an NxM array.\n");
    printf("Enter the size of the array (Row Column): ");
    scanf(" %d %d", &N, &M);

    int array[N][M];     

    // Array Input
    for (row = 0; row < N; row++)
    {    
        printf("Enter row %d: ", row);      
        for (col = 0; col < M; col++); 
        {    
            scanf(" %d", &array[row][col]);
        }   
    }    
    printf("\n");    
    return 0;
}

然后节目就完全结束了。我真的不知道为什么它会被跳过。我尝试过改变scanf()语句中的空格数量,但无论我做了什么更改,都会出现相同的问题。我不确定我是否错过了一些愚蠢的错误,或者是否有更大的问题。我对编码相当陌生

您只需要一个零散的分号。试着改变

for (col = 0; col < M; col++); 
{
for(col=0;col
致:

for(col=0;col
for
引入了一条控制下一条语句的语句,它可以是以分号结尾的简单语句、另一条控制语句或使用
{
大括号
}的复合语句
。在您的代码中,与
关键字的
在同一行的分号视为无效的空语句。因此,代码只会将循环变量增加适当的次数,然后再继续下一部分

确保启用编译器警告。(gcc和clang都提供了明确的警告,说明此代码可能无法达到您的目的。)

for(col=0;col
这是您在nest for循环中编写的内容。但是“;”表示代码行的结尾,它将只运行for循环M次,而不做任何操作

换成

for (col = 0; col < M; col++)
for(col=0;col
非常简单,嗯……哈哈哈哇……非常感谢你,我要花很长时间才能看到这个额外的分号。我可能犯了编码中最初级的错误,对此我深表歉意。@TannerMiller“初级错误”这不是
问题。这是最后一点:不启用所有编译器警告或使用弱编译器。@chux对于类,我们需要使用运行远程linux桌面的NoMachine gcc编译器。我不完全确定它是否是一个好的编译器,我将使用它来确保启用所有警告。@GoldMonkeyTMM您也可以始终使用另一个编译器-即使只是为了获得良好的编译器错误/警告反馈。良好启用的
gcc
应该足够好。注意:
scanf(“%d”…
scanf(“%d”…
)扫描相同(无空格)。
for (col = 0; col < M; col++)
{
for (col = 0; col < M; col++);
for (col = 0; col < M; col++)