gpt4 book ai didi

c - C中的循环出错了

转载 作者:太空宇宙 更新时间:2023-11-04 01:29:49 25 4
gpt4 key购买 nike

我有这个代码

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] 获取值

简而言之,当i更改值时,Runnerm的先前存储值将获取当前存储的 Runners 的值..

这是为什么呢?我找不到原因..

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

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

最佳答案

看来您的 printf 语句显示的是下一个运行者/下一次,但记录之前的运行者/时间:

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

为什么不是 i j

关于c - C中的循环出错了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24459312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com