2015-12-22 86 views
1

當我有關scanf()雖然使用多個scanf()的它跳過scanf()的其餘

它只是跳過第二,第三和之後的任何其他scanf()輸入一個值。

這是我的代碼:

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int main() 
{ 
    float manyTest, gr1, gr2, gr3, gr4, gr5, gr6, gr7, gr8; 
    manyTest = gr1 = gr2 = gr3 = gr4 = gr5 = gr6 = gr7 = gr8 = 0; 

    printf("How many tests you wanna average? (Minimum 1 Maximum 8)\n"); 
    scanf(" %f", &manyTest); 

    if (manyTest <= 0) { 
     printf("The Minimum is 1!\n");  
    } else 
    if (manyTest > 8) { 
     printf("The Maximum is 8!\n"); 
    } else { 
     if (manyTest = 1) { 
      printf("Write down your grades on those tests:\n"); 
      scanf(" %f", &gr1); 
     } else 
     if (manyTest = 2) { 
      printf("Write down your grades on those tests:\n"); 
      scanf(" %f", &gr1); 
      scanf(" %f", &gr2); 
     } else 
     if (manyTest = 3) { 
      printf("Write down your grades on those tests:\n"); 
      scanf(" %f", &gr1); 
      scanf(" %f", &gr2); 
      scanf(" %f", &gr3);  
     } else 
     if (manyTest = 4) { 
      printf("Write down your grades on those tests:\n"); 
      scanf(" %f", &gr1); 
      scanf(" %f", &gr2); 
      scanf(" %f", &gr2); 
      scanf(" %f", &gr3); 
      scanf(" %f", &gr4); 
     } else 
     if (manyTest = 5) { 
      printf("Write down your grades on those tests:\n"); 
      scanf(" %f", &gr1); 
      scanf(" %f", &gr2); 
      scanf(" %f", &gr2); 
      scanf(" %f", &gr3); 
      scanf(" %f", &gr4); 
      scanf(" %f", &gr5); 
     } else 
     if (manyTest = 6) { 
      printf("Write down your grades on those tests:\n"); 
      scanf(" %f", &gr1); 
      scanf(" %f", &gr2); 
      scanf(" %f", &gr2); 
      scanf(" %f", &gr3); 
      scanf(" %f", &gr4); 
      scanf(" %f", &gr5); 
      scanf(" %f", &gr6); 
     } else 
     if (manyTest = 7) { 
      printf("Write down your grades on those tests:\n"); 
      scanf(" %f", &gr1); 
      scanf(" %f", &gr2); 
      scanf(" %f", &gr2); 
      scanf(" %f", &gr3); 
      scanf(" %f", &gr4); 
      scanf(" %f", &gr5); 
      scanf(" %f", &gr6); 
      scanf(" %f", &gr7); 
     } else 
     if (manyTest = 8) { 
      printf("Write down your grades on those tests:\n"); 
      scanf(" %f", &gr1); 
      scanf(" %f", &gr2); 
      scanf(" %f", &gr2); 
      scanf(" %f", &gr4); 
      scanf(" %f", &gr5); 
      scanf(" %f", &gr6); 
      scanf(" %f", &gr7); 
      scanf(" %f", &gr8); 
     } 
     float avg = (gr1 + gr2 + gr3 + gr4 + gr5 + gr6 + gr7 + gr8)/manyTest; 
     printf("Your average grade is: %.2f\n", avg); 
    } 
    system("pause"); 
    return 0; 
} 

什麼是錯我的代碼,它跳過scanf()的休息嗎? 我想要的代碼是詢問他們想要測試多少次,然後取這個數字(在1到8之間),然後進行測試,然後平均分數。

讓說:

How many test...? 
2 
Write down your grades: 
100 
90 
Your average grade is: 95.00 

,但它確實是:

How many test.....? 
2 
Write down your grades: 
90 
Your average is: 90.00 

,甚至不讓它到另一個變量的信息。

+0

爲什麼在它們每個中的'%f'之前的空格?刪除它們。 – ameyCU

+0

爲什麼不使用數組和循環? –

+0

另外,您需要了解賦值('=')和比較等號('==')之間的區別。 –

回答

2

其實,在你的代碼,罪魁禍首是不是scanf但這些wrong方程

else if (manyTest = 8) //WROOOOONNGGGG!! =(=(=(=(

,而不是把雙等號...

else if (manyTest == 8) //correct! =) 

因爲你真正想要的是比較

+0

謝謝!有效! – kuyy

+0

太好了。 ;) 很高興聽你這樣說。你可能還想了解'array','for-loop'迭代和'switch-case'塊,以使你的代碼看起來更漂亮......;)不知道老師現在需要什麼... – Ian

1

問題是,下面的語句將永遠匹配第一個if。

首先您需要使用==進行比較,因爲使用=只是將其作爲賦值。

其次,分配manyTest = 1將始終評估爲true,因爲它是如何工作的。所以當你到達if (manyTest = 1)的條件將是真實的,所以你會進入第一個案件,只有一個scanf。你永遠不會達到其他if語句並測試這些條件。

if (manyTest = 1) { 
    printf("Write down your grades on those tests:\n"); 
    scanf(" %f", &gr1); 
} 
else if (manyTest = 2) { 
    printf("Write down your grades on those tests:\n"); 
    scanf(" %f", &gr1); 
    scanf(" %f", &gr2); 
} 
... 

在將來調試時東西,你可以在printf中使用不同的文本在每個if語句,以更好地瞭解事情出錯。

0

。在你的代碼中的錯誤經典:

您使用賦值操作符=,而不是在測試中的比較操作==。 C非常靈活:它允許在測試表達式中進行賦值,這會導致您遇到的經典錯誤。你被咬了一次,所以現在你知道你應該輸入什麼。任何人都可以犯一個錯字,有一個非常有效的方法來防止這個問題:編譯時啓用警告(例如:gcc -Wall -Wextra -Werror)。