2016-04-23 186 views
2

我無法真正解釋它,只不過scanf()只讀取第一個值,然後基於此計算出計算結果。scanf()只讀第一個輸入(數字)

int main() { 
    int i, students = 0; 
    char name[20]; 
    int tests; 
    float test_score; 
    int test_sum = 0; 
    char letter_grade; 
    double test_average; 

    printf("Number of students: "); 
    scanf("%d", &students); 

    for (i = 0; i < students; i++) { 
     printf("\nStudent name %d: ", i + 1); 
     scanf(" %s", &name); 
     fflush(stdin); 

     printf("Number of test(s) for %s: ", name); 
     scanf("%d", &tests); 
     fflush(stdin); 

     printf("Enter %d test score(s) for %s: ", tests, name); 
     if (i < students) { 
      scanf("%f", &test_score); 
      test_sum += test_score; 
      test_average = test_sum/(float)tests; 
     } 
     printf("Average test score: %.2f", test_average); 

     fflush(stdin); 

    } 
    return 0; 
} 

說我進入2名學生,有2測試成績第一的學生,然後進入45 87.我應該得到66.00,但我得到22.50。對於第二個學生,我會輸入100 55 87的3個測試分數,而我得到48.33。 Waaayyy關閉。

我知道我做錯了什麼,但我無法弄清楚,因爲我之前工作過,但循環不會繼續給第二個學生。

+2

你知道什麼'fflush(stdin)'做? – EOF

+0

'if(i for(int j = 0; j BLUEPIXY

+0

@EOF清除緩衝區。當我不包含它時,我的程序結束。我確實看到它是多麼的多餘,但我不知道爲什麼我的代碼不會沒有它們。 – DSmith

回答

0
if (i < students) { 
    scanf("%f", &test_score); 
    test_sum += test_score; 
    test_average = test_sum/(float)tests; 
} 

應該是:

test_sum = 0; 
for (int j = 0; j < tests; j++) { 
    scanf("%f", &test_score); 
    test_sum += test_score; 
} 
test_average = test_sum/(float)tests; 
+0

這似乎是獲得正確計算的唯一方法,但是我的程序剛剛結束,並且沒有到達#2學生。我也刪除了fflush(stdin)。我已經圈了幾個小時...... – DSmith

+0

test_sum變量在for循環之前應該設置爲0 –

1

您總是需要檢查返回值scanf()以查看它讀取的令牌數量。如果閱讀失敗,則需要採取糾正措施。

不清楚爲什麼每次都需要fflush(stdin)

0

張貼的代碼包含了幾個問題,包括

  1. 只能輸入一個測試得分
  2. int隨機搭配和floatdouble變量
  3. 註冊CT格式字符串調用scanf()
  4. 許多未用的變量
  5. 故障檢查錯誤上調用scanf()
  6. 差變量命名。變量名應說明的內容或使用(或更好兩者)
  7. 調用fflush(stdin)被明確列爲C標準
  8. test_sum未定義行爲是不是學生

之間重新初始化以下建議代碼修復所有上面的問題和編譯乾淨

#include <stdio.h> 
#include <stdlib.h> // exit(), EXIT_FAILURE 

// prototypes 
void flushStdin(void); 

int main(void) 
{ 
    int numStudents = 0; 
    char studentName[20]; 
    int numTests; 

    double test_score; 
    double test_sum = 0.0; 
    //char letter_grade; 
    double test_average; 

    printf("Number of students: "); 
    if(1 != scanf("%d", &numStudents)) 
    { // then scanf failed 
     perror("scanf for number of students failed"); 
     exit(EXIT_FAILURE); 
    } 

    // implied else, scanf successful 

    flushStdin(); 

    for (int i = 0; i < numStudents; i++) 
    { 
     printf("\nStudent name %d: ", i + 1); 
     if(1 != scanf(" %s", studentName)) 
     { // then scanf failed 
      perror("scanf for student name failed"); 
      exit(EXIT_FAILURE); 
     } 

     // implied else, scanf successful 

     flushStdin(); 

     printf("Number of test(s) for %s: ", studentName); 
     if(1 != scanf("%d", &numTests)) 
     { // scanf failed 
      perror("scanf for number of tests failed"); 
      exit(EXIT_FAILURE); 
     } 

     // implied else, scanf successful 

     test_sum = 0.0; 
     printf("Enter %d test score(s) for %s: ", numTests, studentName); 
     for(int j=0; j<numTests; j++) 
     { 
      if(1 != scanf("%lf", &test_score)) 
      { // then scanf failed 
       perror("scanf for test score failed"); 
       exit(EXIT_FAILURE); 
      } 

      // implied else, scanf successful 

      flushStdin(); 

      test_sum += test_score; 
     } 

     test_average = test_sum/numTests; 
     printf("Average test score: %.2lf", test_average); 
    } 
    return 0; 
} // end function: main 


void flushStdin() 
{ 
    int ch; 
    while((ch = getchar()) != EOF && '\n' != ch); 
}