2013-08-19 42 views
1

我遇到了我的代碼問題。我的課程是一個招生系統,每次我選擇一個開關盒時,它應該顯示總價格,但在選擇最後一門課程後,總價格增加看起來是錯誤的。請幫忙。加法和賦值錯誤

#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    int n, none, ntwo, nthree, Total; 
    float Algebra, Trigonometry, Calculus, Engiana, Physics; 
    char password[20], username[8]; 
    Algebra = 100; 
    Trigonometry = 300; 
    Calculus = 500; 
    Engiana = 750; 
    Physics = 1500; 
    Total = (none + ntwo + nthree); 

    printf("Welcome to the Enrollment System \n"); 
    printf("Here is the list of Available Subjects \n"); 
    printf("\n"); 
    printf("Course Code   Price\n"); 
    printf("\n"); 
    printf("1. Algebra    %.2f \n",Algebra); 
    printf("2. Trigonometry  %.2f \n",Trigonometry); 
    printf("3. Calculus   %.2f \n",Calculus); 
    printf("4. Engiana    %.2f \n",Engiana); 
    printf("5. Physics    %.2f \n",Physics); 

    printf("You can only select three courses to enroll for the Term \n"); 
    printf("\n Select First Course to Enroll \n"); 
    scanf("%d", &none); 

    switch(none) 
    { 
     case 1: printf(" You Enrolled Algebra %.2f \n",Algebra); break; //if conditions met , immediately goes to system pause 
     case 2: printf(" You Enrolled Trigonometry %.2f \n", Trigonometry); break; //if conditions not met goes to another case 
     case 3: printf(" You Enrolled Calculus %.2f \n", Calculus); break; 
     case 4: printf(" You Enrolled Engiana %.2f \n", Engiana); break; 
     case 5: printf(" You Enrolled Physics %.2f \n", Physics); break; 
     default: printf(" The Course you entered is not valid \n"); break; 
    } 
    printf("\n Select Second Course to Enroll \n"); 
    scanf("%d", &ntwo); 

    switch(ntwo) 
    { 
     case 1: printf(" You Enrolled Algebra %.2f \n",Algebra); break; //if conditions met , immediately goes to system pause 
     case 2: printf(" You Enrolled Trigonometry %.2f\n", Trigonometry); break; //if conditions not met goes to another case 
     case 3: printf(" You Enrolled Calculus %.2f\n", Calculus); break; 
     case 4: printf(" You Enrolled Engiana %.2f\n", Engiana); break; 
     case 5: printf(" You Enrolled Physics %.2f \n", Physics); break; 
     default: printf(" The Course you entered is not valid \n"); break; 
    } 

    printf("\n Select Third Course to Enroll \n"); 
    scanf("%d", &nthree); 
    switch(nthree) 
    { 
     case 1: printf(" You Enrolled Algebra %.2f \n",Algebra); break; //if conditions met , immediately goes to system pause 
     case 2: printf(" You Enrolled Trigonometry %.2f \n", Trigonometry); break; //if conditions not met goes to another case 
     case 3: printf(" You Enrolled Calculus %.2f \n", Calculus); break; 
     case 4: printf(" You Enrolled Engiana %.2f \n", Engiana); break; 
     case 5: printf(" You Enrolled Physics %.2f \n", Physics); break; 
     default: printf(" The Course you entered is not valid \n"); break; 
    } 

    printf("Total Tuition Price = %.2f \n",Total); 
    system("PAUSE"); 
    return 0; 
} 
+0

'代數''等是'int'(因爲它們沒有類型),但你似乎把它們當作float('「...%.2f \ n」,代數「')? – trojanfoe

+0

@trojanfoe它們是'float',line #2功能 –

+0

@KarthikT糟糕 - 錯過了... – trojanfoe

回答

2
Total = (none + ntwo + nthree); 

此行應該放在最後。

即你的代碼的底部應該是這樣的

Total = (none + ntwo + nthree); 
    printf("Total Tuition Price = %.2f \n",Total); 
    system("PAUSE"); 
    return 0; 
} 

請記住,這些指令在它們被寫入的順序執行,並在您的原代碼,用戶有機會之前計算Total回答。

順便說一句,我明白你可能剛剛開始學習,但這是我遵循的格言,如果你粘貼的不僅僅是你自己的代碼行,你可以做得更好。一旦你找到函數,數組和結構體,也許你會重新考慮這個,並嘗試去掉任何重複的代碼。

另外兩位回答者指出了同樣有效的問題,您目前只是添加了用戶輸入,而您可能希望自己添加課程的費用(?)。

1

你有一個邏輯錯誤。您需要在您的scanf之後每次放置變量Total的賦值語句。但是您必須在switch語句中更新合適的金額。

例如:

case 1: printf(" You Enrolled Algebra %.2f \n",Algebra); Total += 100; break; //if conditions met , immediately goes to system pause 
    case 2: printf(" You Enrolled Trigonometry %.2f \n", Trigonometry); Total += 300; break; //if conditions not met goes to another case 
    case 3: printf(" You Enrolled Calculus %.2f \n", Calculus); Total += 500; break; 
    case 4: printf(" You Enrolled Engiana %.2f \n", Engiana);Total += 750; break; 
    case 5: printf(" You Enrolled Physics %.2f \n", Physics); Total += 1500; break; 
    default: printf(" The Course you entered is not valid \n"); break; 
    } 
     printf("\n Select Second Course to Enroll \n"); 
     scanf("%d", &ntwo);` 

PS:您的代碼可以使用循環來改善..

1

主要有2個錯誤

Total變量是int數據類型。所以在打印時,應該使用%d。對於使用%.2f你應該讓Total變量float

爲了找到總學費的價格應該在每一種情況下增加學費這樣

case 1:Total += Algebra; printf(" You Enrolled Algebra %.2f \n",Algebra); break; //if conditions met , immediately goes to system pause 
case 2:Total += Trigonometry; printf(" You Enrolled Trigonometry %.2f \n", Trigonometry); break; //if conditions not met goes to another case 
case 3:Total += Calculus; printf(" You Enrolled Calculus %.2f \n", Calculus); break; 
case 4:Total += Engiana; printf(" You Enrolled Engiana %.2f \n", Engiana); break; 
case 5:Total += Physics; printf(" You Enrolled Physics %.2f \n", Physics); break; 
default: printf(" The Course you entered is not valid \n"); break; 

你也應該intialise Total以0代替(none + ntwo + nthree)