简体   繁体   English

for循环永远不会以c结尾

[英]for loop never ends in c

I'm trying to write ac program that prints a grid for each year. 我正在尝试编写一个交流程序,该程序每年打印一个网格。 I have a for loop that iterates prints the grid for each year for how many years you choose in the argument. 我有一个for循环,可以循环打印每年在参数中选择多少年的网格。 The for loop prints the grid for an endless number of years exceeding the boundary put on it for some reason. for循环出于某些原因超出了设置的边界打印网格多年。 Here's the code: 这是代码:

    int main(int argc, char *argv[]) {
if (argc != 3) /* argc should be 2 for correct execution */
{
    /* We print argv[0] assuming it is the program name */
    printf("usage: %s filename", argv[0]);
} else {
    int year = atoi(argv[1]);
    double gridA[11][11];
    double gridB[11][11];
    int in;
    int n;
    printf("%d\n",year);
    FILE *file = fopen(argv[2], "r");
    for (int i = 0; i < 12; i++) {
        fscanf(file, "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
                &gridA[i][0], &gridA[i][1], &gridA[i][2], &gridA[i][3],
                &gridA[i][4], &gridA[i][5], &gridA[i][6], &gridA[i][7],
                &gridA[i][8], &gridA[i][9], &gridA[i][10], &gridA[i][11]);
    }
    fclose(file);
    for(n = 0; n < year; n++) {
        printf("Year %d: \n", n);
        if (n == 0) {
            for (int i = 0; i < 12; i++) {
                for (int j = 0; j < 12; j++) {
                    if (j == 11) {
                        printf("%.1lf\n", gridA[i][j]);
                    } else {
                        printf("%.1lf ", gridA[i][j]);
                    }
                }
            }
        } else if (n % 2) {
            in = nextDependency(gridA, gridB);
            for (int i = 0; i < 12; i++) {
                for (int j = 0; j < 12; j++) {
                    if (j == 11) {
                        printf("%.1lf\n", gridB[i][j]);
                    } else {
                        printf("%.1lf ", gridB[i][j]);
                    }
                }
            }
        } else {
            in = nextDependency(gridB, gridA);
            for (int i = 0; i < 12; i++) {
                for (int j = 0; j < 12; j++) {
                    if (j == 11) {
                        printf("%.1lf\n", gridA[i][j]);
                    } else {
                        printf("%.1lf ", gridA[i][j]);
                    }
                }
            }
        }
    }
}
exit(0);
}

The for loop that never ends is this one: 永无止境的for循环是这样的:

for(n = 0; n < year; n++) {
printf("Year %d: \n", n); ...

Through attempting to debug I found that the loop is finite when before this code: 通过尝试调试,我发现在此代码之前,循环是有限的:

FILE *file = fopen(argv[2], "r");
    for (int i = 0; i < 12; i++) {
        fscanf(file, "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
                &gridA[i][0], &gridA[i][1], &gridA[i][2], &gridA[i][3],
                &gridA[i][4], &gridA[i][5], &gridA[i][6], &gridA[i][7],
                &gridA[i][8], &gridA[i][9], &gridA[i][10], &gridA[i][11]);
    }

But when put after that code it loops infinitely, this is a bug I cannot figure out why it is happening? 但是当放在该代码之后它无限循环时,这是一个我无法弄清为什么会发生的错误? Does anyone have any idea how I could fix this? 有谁知道我该如何解决?

You've defined your grid with dimensions 11 by 11, but you're reading 12 elements into it. 您已经定义了尺寸为11 x 11的网格,但是正在其中读取12个元素。 The last element overwrites your loop variable. 最后一个元素将覆盖您的循环变量。

In general, if you define an array of size n , you can access elements 0 through n-1 . 通常,如果定义大小为n的数组,则可以访问元素0n-1

In this case, the solution is to define the grids with space for all 12 elements. 在这种情况下,解决方案是为所有12个元素定义具有空间的网格。

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

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