简体   繁体   English

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

[英]Why is scanf() being skipped over in this nested for loop in language C?

I am writing a program for class that asks users to enter a the size of a 2D array and then has the user enter the values for the array. 我正在编写一个类的程序,要求用户输入2D数组的大小,然后让用户输入数组的值。 This is the code I have so far: 这是我到目前为止的代码:

#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;
}

The issue that I am having when the program is compiled and the information is entered, the second and all following scanf() statements are skipped over or the whole second for loop is skipped over after the first line is entered. 输入第一行后,在编译程序并输入信息时,我遇到的问题是跳过第二个及所有随后的scanf()语句,或者跳过整个第二个for循环。 Here is an example of entered input: 这是输入的示例:

This program counts occurences of digits 0 through 9 in an NxM array.
Enter the size of the array (Row Column): 2 6
Enter row 0: 0 1 2 3 4 5
Enter row 1:

And then the program ends entirely. 然后程序完全结束。 I really have no idea why its being skipped over. 我真的不知道为什么要跳过它。 I have tried altering how many spaces are in the scanf() statement but no matter what I change the same issue occurs. 我尝试更改scanf()语句中的空格,但是无论我如何更改,都会发生相同的问题。 I am unsure if I am missing some stupid mistake I have made or if there is a bigger problem. 我不确定是否遗漏了我犯的一些愚蠢的错误,或者还有更大的问题。 I am pretty new at coding. 我是编码方面的新手。

You just have a stray semicolon. 您只是流浪的分号。 Try changing 尝试改变

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

to: 至:

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

A for introduces a statement which controls the very next statement, which could be a simple statement ending with a semicolon, another control statement, or a compound statement using { braces } . for引入了一个语句,该语句控制下一个语句,该语句可以是以分号结尾的简单语句,另一个控制语句或使用{大括号}的复合语句。 In your code, the semicolon on the same line as the for keyword counts as an empty statement that has no effect. 在您的代码中,与for关键字在同一行的分号被视为无效的空语句。 So that code will just increment the loop variable the appropriate number of times, and only after that move on to the next part. 因此,该代码只会将循环变量递增适当的次数,并且仅在此之后移至下一部分。

Make sure you enable compiler warnings. 确保启用编译器警告。 (Both gcc and clang provide clear warnings that this code might not do what you meant.) (gcc和clang都提供了明确的警告,表明该代码可能无法满足您的意图。)

for (col = 0; col < M; col++);

This is what you wrote inside the nest for loop. 这就是您在嵌套for循环中编写的内容。 But the ";" 但是“;” simply means the end of code line and it will just run the for loop M times without doing anything. 只是表示代码行的末尾,它只会执行for循环M次,而无需执行任何操作。

Change it to 更改为

for (col = 0; col < M; col++)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM