2014-06-27 57 views
1

我有這樣的代碼循環使用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]使用的內存?

+1

數組是如何定義的? – Leeor

+0

int Runnerm [s] [4]; //跑步者/分鐘 int Runners [s] [4]; //跑步者/秒 – SpK

+0

你是說從'檢查1'到'檢查1.5','i'改變了數值,也就是增加了'1'? –

回答

2

看來,你的printf語句顯示下一個選手/接下來的時間,但在此之前錄製亞軍/時間:

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

爲什麼不ij

+0

好的。愛那些基地0基地1問題:) –

+0

發生在我們所有:-) –

+0

這個問題是故意放在,我猜!太令人沮喪了! –

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]); 

to outside the j loop,r消除冗餘提示,擺脫用作循環邊界的魔術常量。

#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]); 
} 

雖然就個人而言,我認爲nonfunctionality是至少你的問題:你的代碼是overcommented浸泡WET,而您的輸出中有太多的換行符。

+0

檢查不是問題,我必須說多少次?檢查存在檢查值,即使沒有檢查1或檢查1.5,值仍然改變:O – SpK

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> 

它似乎工作正常。

+0

你可以做兩個跑步者,看看它是否運行相同?因爲問題只有當有超過2名選手時纔會出現 – SpK

+0

有你去...... 3名選手。 –

+0

但是我得到了同樣的錯誤結果,我已將副本粘貼到eclipse中,並且仍然是runnerm更改:\ – SpK