我想要運行下面的程序..但它不斷給我錯誤在結束..似乎我忘了關閉一個循環或如此..如果有人能夠幫助我,這將是偉大的!錯誤;預期表達式'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;
,如果您有任何建議,請讓我知道..在這一點上,我不知道我做錯了
這不是有效的C++反正。它需要'int main'。我認爲你要找的是一個開關。 – chris
爲什麼你使用for循環而不是簡單的if語句(是的,開關會更好)? – Dukeling
爲什麼使用for-loops的原因是因爲它是在這個assigment中特別要求的。 – user2803386