2013-09-22 97 views
0

我想要運行下面的程序..但它不斷給我錯誤在結束..似乎我忘了關閉一個循環或如此..如果有人能夠幫助我,這將是偉大的!錯誤;預期表達式'else'

#include <stdio.h> 
#include <math.h> 

main() { 

//variables 
int user_selection, count_donate=0, count_invest=0; 
float balance_of_fund, donate_amt, invest_amt; 

//input 
printf("Welcome!\n"); 
printf("What is the initial balance of the fund?\n"); 
scanf("%f", balance_of_fund); 

//provide user with options 
printf("What would you like to do?\n 1 - Make a donation\n 2 - Make an investment\n 3 -   
Print balance of fund\n 4 - Quit\n"); 


//work with user selectiom 
scanf ("%d", user_selection); 

if (user_selection > 0 && user_selection < 4) { 

     //ask for donation amount 
     for (user_selection = 1; user_selection<=1; user_selection++) { 
        printf("How much would you like to donate?\n"); 
        scanf("%f", donate_amt); 
        balance_of_fund = balance_of_fund + donate_amt; 
        count_donate++; 
     } 

     //ask for investment amount 
     for (user_selection = 2; user_selection<=2; user_selection++) { 
        printf("How much would you like to invest?\n"); 
        scanf ("%f", invest_amt); 
        balance_of_fund = balance_of_fund - invest_amt; 
        count_invest++; 
     } 

     //print out final balance for selection 3 
     for (user_selection = 3; user_selection<=3; user_selection++) { 
        printf("The final balance is $%.2f.", balance_of_fund); 
     printf("There were %d donations and %d investments.",count_donate,    count_invest); 
     } 

     //Quit for selection 4 
     else if (user_selection = 4) { 
        printf("The final balance is $%.2f." ,balance_of_fund); 
     } 

else { 
     printf("Please make a valid selection"); 
    } 
return 0; 

,如果您有任何建議,請讓我知道..在這一點上,我不知道我做錯了

+1

這不是有效的C++反正。它需要'int main'。我認爲你要找的是一個開關。 – chris

+0

爲什麼你使用for循環而不是簡單的if語句(是的,開關會更好)? – Dukeling

+0

爲什麼使用for-loops的原因是因爲它是在這個assigment中特別要求的。 – user2803386

回答

2

你的第一個else沒有if它。這一個

//Quit for selection 4 
    else if (user_selection = 4) { 

而且,@Yu浩在C++的意見,平等比較操作注意是==,不=

前面的for循環背後的想法也逃脫了我。你真的明白什麼for陳述嗎?我強烈懷疑你沒有。

如果您是else前添加},應適當平衡if的和else「在你的代碼,但它仍然不會讓那些for的做任何有意義。

+0

也'''應該'=='。 –

0

這是你所提供的代碼的結構:

if (...) { 
    for (...) { 
     ... 
    } 
    // there is no if preceding else on the next line 
    else if (...) { 
     ... 
    } 
// missing }, scope of the first if is not closed properly 
else { 
    ... 
} 

而且這條線:

for (user_selection = 1; user_selection<=1; user_selection++) { 

使循環完全無用的,也就是說,它是同樣喜歡就沒有循環,只是另一個範圍:

{ 
0

你錯過了一個右括號「}」,如果只是爲了e其他如果和「退出選擇4」評論。

0

使用switch情況下,而不是供用戶選擇

switch(user_selection) 
{ 
case 1: 
//do something i.e. make donation 
case 2: 
//do something 
. 
. 
. 
default: 
} 

編輯:試試下面的代碼。我唯一添加的是},你錯過了,'&'在%d%f之前標誌。

#include <stdio.h> 
#include <math.h> 

main() 
{ 
     //variables 
     int user_selection, count_donate=0, count_invest=0; 
     float balance_of_fund, donate_amt, invest_amt; 

     //input 
     printf("Welcome!\n"); 
     printf("What is the initial balance of the fund?\n"); 
     scanf("%f",&balance_of_fund); 

     //provide user with options 
     printf("What would you like to do?\n 1 - Make a donation\n 2 - Make an investment\n 3 -Print balance of fund\n 4 - Quit\n"); 


     //work with user selectiom 
     scanf ("%d",&user_selection); 

     if (user_selection > 0 && user_selection < 4) 
     { 

        //ask for donation amount 
        for (user_selection = 1; user_selection<=1; user_selection++) 
        { 
           printf("How much would you like to donate?\n"); 
           scanf("%f",&donate_amt); 
           balance_of_fund = balance_of_fund + donate_amt; 
           count_donate++; 
        } 

       //ask for investment amount 
        for(user_selection = 2; user_selection<=2; user_selection++) 
        { 
           printf("How much would you like to invest?\n"); 
           scanf ("%f",&invest_amt); 
           balance_of_fund = balance_of_fund - invest_amt; 
           count_invest++; 
        } 

        //print out final balance for selection 3 
        for (user_selection = 3; user_selection<=3; user_selection++) 
        { 
           printf("The final balance is $%.2f.", balance_of_fund); 
           printf("There were %d donations and %d investments.",count_donate,count_invest); 
        } 
     } 
    //Quit for selection 4 
     else if (user_selection == 4) 
     { 
       printf("The final balance is $%.2f.",balance_of_fund); 
     } 
     else 
     { 
        printf("Please make a valid selection"); 
     } 
     return 0; 

}

+0

我同意,但他們期待我使用for循環的這個任務 – user2803386

+0

你應該已經提到,清楚地在 –

+0

的問題再次檢查答案我已編輯,下次您發佈任何問題時請確保您也包含錯誤。 –