[英]Loop in C gone wrong
I have this code 我有这个代码
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!
}
}
The problem is, when check 1.5 runs the second time, Runnerm[i][j]
gets values from Runners[i+1][j]
问题是,当检查1.5第二次运行时,
Runnerm[i][j]
从Runners[i+1][j]
获取值
In short, when i changes value, the previous stored value of Runnerm
will get the value of the Runners that is currently being stored.. 简而言之,当我更改值时,
Runnerm
的先前存储的值将获得当前正在存储的Runners的值。
Why is that? 这是为什么? I can't find the reason..
我找不到原因。
Edit: The "check 1" and "check 1.5" exist for the whole purpose of -checking- the values at any given time of the loop. 编辑: “检查1”和“检查1.5”的存在是为了在循环的任何给定时间“检查”值。 SO, even if I delete them, the program will still be broken.
因此,即使删除它们,该程序仍然会损坏。 ALSO when this is all fixed, the checks will be deleted, because I won't need to check the values any more.
同样,当这一切都固定后,检查将被删除,因为我不再需要检查这些值。 Thank you all for answering but I don't get why you suggest to do something with the checks, they are there for debugging.
谢谢大家的回答,但我不明白为什么您建议对检查做一些事情,它们在那里可以调试。 :\\
:\\
Edit Found what caused the problem, when I used another version of the program that used fewer arrays, the problem was solved, when I started adding random un-used arrays, the problem appears. 编辑发现问题的原因,当我使用另一个使用较少数组的程序版本时,问题得以解决,当我开始添加随机未使用的数组时,出现了问题。 Why is that?
这是为什么? (New problem) Could it be that the memory overlaps to the one that Runners[i][j] use?
(新问题)可能是内存与Runners [i] [j]使用的内存重叠吗?
It appears that your printf
statement is showing the next runner/next time, but recording for the runner/time before: 您的
printf
语句似乎显示了下一个跑步者/下一次时间,但记录了之前的跑步者/时间:
printf("\nEnter the time of lap %d for runner %d in minutes: ", j+1, i+1);
^^^^ ^^^^
Why not i
j
为什么不是
i
j
Move these lines of code: 移动以下代码行:
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]);
to outside the j
loop, remove the redundant prompt, and get rid of the magic constants used as loop bounds. 在
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]);
}
Although personally, I think that nonfunctionality is the least of your problems: your code is overcommented and soaking WET , and your output has way too many line breaks in it. 虽然就我个人而言,但我认为非功能性是您遇到的最少问题:您的代码过分注释并且吸收了WET ,并且您的输出中有太多的换行符。
Fleshed-out the question code to an actual program: 将问题代码整理到实际程序中:
#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!
Added the following if(j > ...
in order to not print uninitialized array values: 添加了以下
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);
}
Then I compiled the code: 然后我编译了代码:
SLES11SP2:~/SO> gcc -Wall -o test *.c
And ran the code: 并运行代码:
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>
It seem to work fine. 它似乎工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.