0

我正在尝试运行以下程序..但它最后一直给我错误..似乎我忘记关闭一个循环左右..如果有人能够帮助我,那就太好了! !

#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;

如果您有任何建议,请告诉我.. 在这一点上,我不确定我做错了什么

4

4 回答 4

2

你的第一个else没有if。这个

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

此外,正如@Yu Hao 在评论中指出的那样,C++ 中的相等比较运算符是==, not =

前面几个for周期背后的想法也让我无法理解。你真的了解for语句的作用吗?我强烈怀疑你没有。

如果您在此}之前添加 a else,它应该在您的代码中正确平衡if's 和else' ,但它仍然不会使这些for' 做任何有意义的事情。

于 2013-09-22T03:19:28.253 回答
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++) {

使循环完全无用,即就像没有循环一样,只是另一个范围:

{
于 2013-09-22T03:20:34.103 回答
0

在 else if 和“Quit for selection 4”注释之前,您缺少 if 的右大括号“}”。

于 2013-09-22T03:22:55.770 回答
0

switch例代替用户选择

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

编辑:试试下面的代码。我唯一添加的是一个}你错过的和 '&' 在 and 之前的%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;

}

于 2013-09-22T03:26:06.650 回答