在C循环出错了

时间:2014-06-27 19:17:46

标签: c arrays loops variables

我有这段代码

for (i = 0; i < s; i++) // for i from 0 to Runners
{
    for (j = 0; j < 4; j++) //for j from 0 to laps
    {
        printf("\nEnter the time of lap %d for runner %d in minutes: ", j+1, i+1); // prompt for time for each runner in minutes
        while (scanf("%d", &Runnerm[i][j]) != 1) // While scanf returns not equal to 1 (value parsed == TRUE)
        {
            while ((ch = getchar()) != '\n') putchar(ch); // check if ch=character, compare last entry (\n if character)
            printf(" is not an integer.\nPlease enter only an "); // print error
            printf("integer, such as 1, 5, or 9 : \n"); // cnt print error
        }

        printf("Enter the time of lap %d for runner %d in seconds: ", j+1, i+1); //prompt for time for each runner in seconds
        while (scanf("%d", &Runners[i][j]) != 1) // While scanf returns not equal to 1 (value parsed == TRUE)
        {
            while ((ch = getchar()) != '\n') putchar(ch); // check if ch=character, compare last entry (\n if character)
            printf(" is not an integer.\nPlease enter only an "); // print error
            printf("integer, such as 1, 5, or 9 : \n"); // cnt print error
        }

        printf(" \n check 1 \n ");
        printf("\n %d minutes -- %d seconds \n", Runnerm[i][j], Runners[i][j]); // Correct Check!

        printf(" \n check 1.5 \n ");
        printf("\n %d minutes -- %d seconds \n", Runnerm[i][0], Runners[i][0]); // Incorrect Check!
        printf("\n %d minutes -- %d seconds \n", Runnerm[i][1], Runners[i][1]); // Incorrect Check!
        printf("\n %d minutes -- %d seconds \n", Runnerm[i][2], Runners[i][2]); // Incorrect Check!
        printf("\n %d minutes -- %d seconds \n", Runnerm[i][3], Runners[i][3]); // Incorrect Check!

    }
}

问题是,当第二次检查1.5时,Runnerm[i][j]Runners[i+1][j]获取值

简而言之,当我更改值时,之前存储的Runnerm值将获得当前存储的Runners的值。

为什么?我找不到原因..

编辑:“检查1”和“检查1.5”的存在是为了在循环的任何给定时间检查值。所以,即使我删除它们,程序仍然会被破坏。 此外,当这一切都修复后,检查将被删除,因为我不再需要检查这些值。谢谢大家回答,但我不明白为什么你建议做一些检查,他们在那里进行调试。 :\

编辑找到导致问题的原因,当我使用其他版本的程序使用较少的数组时,问题就解决了,当我开始添加随机未使用的数组时,问题出现了。 这是为什么? (新问题) 可能是内存与Runners [i] [j]使用的内存重叠了吗?

3 个答案:

答案 0 :(得分:2)

您的printf语句似乎显示下一个跑步者/下一次,但记录的是跑步者/时间之前:

printf("\nEnter the time of lap %d for runner %d in minutes: ", j+1, i+1);
                                                                ^^^^ ^^^^

为什么不i j

答案 1 :(得分:1)

移动以下代码行:

printf(" \n check 1.5 \n ");
printf("\n %d minutes -- %d seconds \n", Runnerm[i][0], Runners[i][0]);
printf("\n %d minutes -- %d seconds \n", Runnerm[i][1], Runners[i][1]);
printf("\n %d minutes -- %d seconds \n", Runnerm[i][2], Runners[i][2]);
printf("\n %d minutes -- %d seconds \n", Runnerm[i][3], Runners[i][3]);

外部 j循环,删除冗余提示,并删除用作循环边界的魔术常量。

#define RUNNERS s
#define LAPS 4

for (i = 0; i < RUNNERS; i++)
{
    for (j = 0; j < LAPS; j++)
    {
        printf("Enter the time of lap %d for runner %d in seconds: ", j+1, i+1)
        while (scanf("%d", &Runners[i][j]) != 1)
        {
            while ((ch = getchar()) != '\n') putchar(ch);
            printf(" is not an integer.\nPlease enter only an integer, such as 1, 5, or 9 : \n");
        }

        printf(" \n check 1 \n ");
        printf("\n %d minutes -- %d seconds \n", Runnerm[i][j], Runners[i][j]);

    }

    printf(" \n check 1.5 \n ");

    for (j = 0; j < LAPS; j++)
        printf("\n %d minutes -- %d seconds \n", Runnerm[i][j], Runners[i][j]);
}

虽然就个人而言,我认为非功能性是您遇到的最少问题:您的代码被过度注释并浸泡WET,并且您的输出中包含太多换行符。

答案 2 :(得分:1)

将问题代码充实为实际程序:

#include <stdio.h>

int main()
  {
  int s = 3;
  int i;
  int j;
  int ch;
  int Runnerm[s][4]; //Runners / minutes
  int Runners[s][4]; //Runners / seconds

  for (i = 0; i < s; i++) // for i from 0 to Runners
     {
     for (j = 0; j < 4; j++) //for j from 0 to laps
        {
        printf("\nEnter the time of lap %d for runner %d in minutes: ", j+1, i+1); // prompt for time for each runner in minutes
        while (scanf("%d", &Runnerm[i][j]) != 1) // While scanf returns not equal to 1 (value parsed == TRUE)
           {
           while ((ch = getchar()) != '\n') putchar(ch); // check if ch=character, compare last entry (\n if character)
           printf(" is not an integer.\nPlease enter only an "); // print error
           printf("integer, such as 1, 5, or 9 : \n"); // cnt print error
           }

        printf("Enter the time of lap %d for runner %d in seconds: ", j+1, i+1); //prompt for time for each runner in seconds
        while (scanf("%d", &Runners[i][j]) != 1) // While scanf returns not equal to 1 (value parsed == TRUE)
           {
           while ((ch = getchar()) != '\n') putchar(ch); // check if ch=character, compare last entry (\n if character)
           printf(" is not an integer.\nPlease enter only an "); // print error
           printf("integer, such as 1, 5, or 9 : \n"); // cnt print error
           }

        printf("check 1 \n ");
        printf("\t %d minutes -- %d seconds \n", Runnerm[i][j], Runners[i][j]); // Correct Check!

        printf("check 1.5 \n ");
        printf("\t %d minutes -- %d seconds \n", Runnerm[i][0], Runners[i][0]); // Incorrect Check!

添加了以下if(j > ...,以便不打印未初始化的数组值:

        if(j > 0) printf("\t %d minutes -- %d seconds \n", Runnerm[i][1], Runners[i][1]); // Incorrect Check!
        if(j > 1) printf("\t %d minutes -- %d seconds \n", Runnerm[i][2], Runners[i][2]); // Incorrect Check!
        if(j > 2) printf("\t %d minutes -- %d seconds \n", Runnerm[i][3], Runners[i][3]); // Incorrect Check!
        }
     }

  return(0);
  }

然后我编译了代码:

SLES11SP2:~/SO> gcc -Wall -o test *.c

并运行代码:

SLES11SP2:~/SO> ./test

Enter the time of lap 1 for runner 1 in minutes: 1
Enter the time of lap 1 for runner 1 in seconds: 2
check 1
   1 minutes -- 2 seconds
check 1.5
   1 minutes -- 2 seconds

Enter the time of lap 2 for runner 1 in minutes: 3
Enter the time of lap 2 for runner 1 in seconds: 4
check 1
   3 minutes -- 4 seconds
check 1.5
   1 minutes -- 2 seconds
   3 minutes -- 4 seconds

Enter the time of lap 3 for runner 1 in minutes: 5
Enter the time of lap 3 for runner 1 in seconds: 6
check 1
   5 minutes -- 6 seconds
check 1.5
   1 minutes -- 2 seconds
   3 minutes -- 4 seconds
   5 minutes -- 6 seconds

Enter the time of lap 4 for runner 1 in minutes: 7
Enter the time of lap 4 for runner 1 in seconds: 8
check 1
   7 minutes -- 8 seconds
check 1.5
   1 minutes -- 2 seconds
   3 minutes -- 4 seconds
   5 minutes -- 6 seconds
   7 minutes -- 8 seconds

Enter the time of lap 1 for runner 2 in minutes: 9
Enter the time of lap 1 for runner 2 in seconds: 10
check 1
   9 minutes -- 10 seconds
check 1.5
   9 minutes -- 10 seconds

Enter the time of lap 2 for runner 2 in minutes: 11
Enter the time of lap 2 for runner 2 in seconds: 12
check 1
   11 minutes -- 12 seconds
check 1.5
   9 minutes -- 10 seconds
   11 minutes -- 12 seconds

Enter the time of lap 3 for runner 2 in minutes: 13
Enter the time of lap 3 for runner 2 in seconds: 14
check 1
   13 minutes -- 14 seconds
check 1.5
   9 minutes -- 10 seconds
   11 minutes -- 12 seconds
   13 minutes -- 14 seconds

Enter the time of lap 4 for runner 2 in minutes: 15
Enter the time of lap 4 for runner 2 in seconds: 16
check 1
   15 minutes -- 16 seconds
check 1.5
   9 minutes -- 10 seconds
   11 minutes -- 12 seconds
   13 minutes -- 14 seconds
   15 minutes -- 16 seconds

Enter the time of lap 1 for runner 3 in minutes: 17
Enter the time of lap 1 for runner 3 in seconds: 18
check 1
   17 minutes -- 18 seconds
check 1.5
   17 minutes -- 18 seconds

Enter the time of lap 2 for runner 3 in minutes: 19
Enter the time of lap 2 for runner 3 in seconds: 20
check 1
   19 minutes -- 20 seconds
check 1.5
   17 minutes -- 18 seconds
   19 minutes -- 20 seconds

Enter the time of lap 3 for runner 3 in minutes: 21
Enter the time of lap 3 for runner 3 in seconds: 22
check 1
   21 minutes -- 22 seconds
check 1.5
   17 minutes -- 18 seconds
   19 minutes -- 20 seconds
   21 minutes -- 22 seconds

Enter the time of lap 4 for runner 3 in minutes: 23
Enter the time of lap 4 for runner 3 in seconds: 24
check 1
   23 minutes -- 24 seconds
check 1.5
   17 minutes -- 18 seconds
   19 minutes -- 20 seconds
   21 minutes -- 22 seconds
   23 minutes -- 24 seconds
SLES11SP2:~/SO> 

似乎工作正常。