我有這樣的代碼循環使用C出了錯
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
的存儲值將獲得當前正在存儲的跑步者的值。
這是爲什麼?我找不到原因..
編輯:「檢查1」和「檢查1.5」的存在是爲了檢查循環中任何給定時間的值的全部目的。所以,即使我刪除它們,程序仍然會被破壞。 此外,當這一切都是固定的,檢查將被刪除,因爲我不需要再檢查值。謝謝大家的回答,但我不明白你爲什麼建議對檢查做些什麼,他們在那裏進行調試。 :\
編輯發現是什麼導致了這個問題,當我使用另一個使用較少數組的程序版本時,問題就解決了,當我開始添加隨機未使用的數組時,問題就出現了。 這是爲什麼? (新問題) 難道是內存重疊到Runners [i] [j]使用的內存?
數組是如何定義的? – Leeor
int Runnerm [s] [4]; //跑步者/分鐘 int Runners [s] [4]; //跑步者/秒 – SpK
你是說從'檢查1'到'檢查1.5','i'改變了數值,也就是增加了'1'? –