2013-02-20 48 views
-1

所以再次我學習新的編程語言這一次卻是C. 而對於我的主菜單創建此功能後,它似乎是在宣告雙「當量「,等同的縮寫,不正確地輸出分配的等價物。我評論了它發現bug的部分。雙聲明沒有什麼是分配給它的C語言編程輸出

下面是該函數的代碼:

// function for grading system 
void gradeSys() 
{ 
    char response; 
    // declaration for computing midterm grades 
    double Mquiz1 = 0, Mquiz2 = 0, Mquiz3 = 0, Mrecit = 0, Massign = 0, MAve = 0, Midex = 0; 

    // declaration for computing final grades 
    double Fquiz1 = 0, Fquiz2 = 0, Fquiz3 = 0, Frecit = 0, Fassign = 0, FAve = 0, Finex = 0; 

    char answer; 
    int counter = 1; 
    // while loop 
    while(counter != 0) { 

    /*!!!---Midterm Grades Input---!!!*/ 
    printf("Welcome to your Grading System.\n"); 
    printf("Please enter your grades for Midterm:\n"); 
    printf("M. Quiz1:\n"); 
    scanf("%d" , &Mquiz1); 
    printf("M. Quiz2:\n"); 
    scanf("%d" , &Mquiz2); 
    printf("M. Quiz3:\n"); 
    scanf("%d" , &Mquiz3); 
    printf("M. Recitation\n"); 
    scanf("%d" , &Mrecit); 
    printf("M. Assignment\n"); 
    scanf("%d" , &Massign); 
    printf("M. Exam:\n"); 
    scanf("%d" , &Midex); 
    // formula for computing midterm grades 
    double Midgrade = 0; 
    MAve = (Mquiz1 + Mquiz2 + Mquiz3 + Mrecit + Massign)/5; 
    double MAveTotal = MAve * .60; 
    double MidExAve = Midex * .40; 
    Midgrade = MAveTotal + MidExAve; 

    printf("Your Midterm average is: %d\n", MAve); 
    printf("Your Midterm Grade is: %d\n", Midgrade); 
    printf("Please press enter to continue...\n"); 
    getchar(); 
    getchar(); 
    system("cls"); 

    /*!!!---Final Grades Input---!!!*/ 
    printf("Please enter your grades for Finals:\n"); 
    printf("F. Quiz1:\n"); 
    scanf("%d" , &Fquiz1); 
    printf("F. Quiz2:\n"); 
    scanf("%d" , &Fquiz2); 
    printf("F. Quiz3:\n"); 
    scanf("%d" , &Fquiz3); 
    printf("F. Recitation\n"); 
    scanf("%d" , &Frecit); 
    printf("F. Assignment\n"); 
    scanf("%d" , &Fassign); 
    printf("F. Exam:\n"); 
    scanf("%d" , &Finex); 
    // formula for computing final grades 
    double Fingrade = 0; 
    FAve = (Fquiz1 + Fquiz2 + Fquiz3 + Frecit + Fassign)/5; 
    double FAveTotal = FAve * .60; 
    double FinExAve = Finex * .40; 
    Fingrade = FAveTotal + FinExAve; 

    printf("Your Final average is: %d\n", FAve); 
    printf("Your Final Grade is: %d\n", Fingrade); 
    printf("Please press enter to continue...\n"); 
    getchar(); 
    getchar(); 
    /*!!!--- Here is where the bug occurs ---!!!*/ 
    // declaration and formula for the term grade 
    double termGrade = 0, equiv; 
    termGrade = (Midgrade + Fingrade)/2; 
    // decision formula for determining equivalent 
    if(termGrade >= 90 && termGrade <= 100) 
    { 
     equiv = 4.0; 
    } 
    else if(termGrade >= 85 && termGrade <= 89) 
    { 
     equiv = 3.0; 
    } 
    else if(termGrade >= 75 && termGrade <= 84) 
    { 
     equiv = 2.0; 
    } 
    else if(termGrade >= 70 && termGrade <= 74) 
    { 
     equiv = 1.0; 
    } 
    else if(termGrade >= 00 && termGrade <= 69) 
    { 
     equiv = 0.0; 
    } 
    else 
    { 
     printf("Invalid grade. Try again.\n"); 
    } 
    // displays the ovarall grade for the term and the equivalent 
    printf("Your grade for this term is: %d. Which is equivalent to a: %d.\n", termGrade, equiv); 
    getchar(); 
    system("cls"); 

    // try again? 
     printf("Do you want to try again:[Y/N]\n"); 
     scanf("%c", &answer); 

     if(answer == 'y' || answer == 'Y') 
     { 
      system("cls"); 
      continue;    
     } 
     else if(answer == 'n' || answer == 'N') 
     { 
      system("cls"); 
      counter = 0; 
     } 
     else 
     { 
      printf("Invalid input\n"); 
     } 

    } 

} 
+0

你不能'printf'雙用'%D'。 – DCoder 2013-02-20 06:42:17

+0

謝謝!像邁克爾說的那樣將它改爲%f,但現在它不會從我的if語句中選擇要分配給「equiv」的值。 – Coolai 2013-02-20 06:50:37

+0

同樣適用於'scanf' ...你也不能使用'%d'讀取一個double。學習如何使用調試器,逐行執行程序,它會比這更快。 – DCoder 2013-02-20 07:02:11

回答

0

%d格式適用於有符號整數。對於浮點數(例如double),您應該使用%f。請參閱http://www.cplusplus.com/reference/cstdio/printf/

或者,您可以將值轉換爲整數(例如(int)FAve)並使用%d(如果要僅打印整個值的部分)。

+0

感謝您的快速響應!將它更改爲%f就像你說的那樣,它工作正常,它現在輸出一個double,但它不會選擇在if語句中輸出的內容。它只輸出一串雙數字(0.00000)。 – Coolai 2013-02-20 06:52:31

+0

「不選」是什麼意思? 「MidGrade」和「Fingrade」的價值是什麼? – Michael 2013-02-20 06:54:08

+0

嗯,其實我沒有在顯示我的Midgrade和Fingrade時出錯。錯誤在if if語句中。 如果條件滿足,我將條件等級分配給條件,假設條件等級爲4.0至0.0。但我認爲它不會去控制語句,只輸出一個0.00000。 – Coolai 2013-02-20 06:55:40