【问题标题】:Problem: Scholarship Endowment Fund Part 2 (fund2.c)问题:奖学金捐赠基金第 2 部分 (fund2.c)
【发布时间】:2021-01-07 01:53:34
【问题描述】:

问题:奖学金捐赠基金第 2 部分 (fund2.c)

我如何返回主菜单并添加一个计数器来计算捐赠和投资的总数,这就是我得到的?

然后,您的程序应该允许用户使用以下选项:

  1. 捐款
  2. 投资
  3. 打印资金余额
  4. 退出

#include #include

//main functions  
int main() {
    // variables 
    int donation, investment, fund, total, tdonations, tinvestments; 

    // prompt user 
    printf("Welcome!\n");
    printf("What is the initial balance of the fund\n");
    scanf("%d", &fund);


    int ans;

    printf("What would you like to do?\n");
    printf("\t1-  Make a Donation\n");
    printf("\t2-  Make an investment\n");
    printf("\t3-  Print balance of fund\n");
    printf("\t4- Quit\n");
    scanf("%d", &ans);


    if (ans == 1) {
    printf("How much would you like to donate?\n");
    scanf("%d", &donation);


    }
     if (ans == 2) {
    printf("How much would you like to invest?\n");
    scanf("%d", &investment);
    return main();
    }
     if (ans == 3) {
        total =  donation + fund - investment;
        if (total < fund) {
            printf("You cannot make an investment of that amount\n");
            return main();
        }
        else {

        printf("The current balance is %d\n", total);
        printf(" There have been %d donations and %d investments.\n", tdonations, tinvestments);

    }
    }
     if (ans == 4) {
        printf("Type 4 to quit\n");
        }
    
    else {
    printf("Not a valid option.\n");
    }

    //switch 
    switch (ans) {
        case 1: 
        printf("How much would you like to donate?\n");
        scanf("%d", &donation);
        return main();
    
        case 2:
        printf("How much would you like to invest\n");
        scanf("%d", &investment);
        return main();

        case 3:
        printf("The current balance is %d\n", total);
        printf(" There have been %d donations and %d investments.\n", tdonations, tinvestments);
        return main();
        case 4:
        break;
    }
    return 0;

}

【问题讨论】:

  • 你知道如何使用循环吗?另外,再次调用main 是不好的形式。
  • 我不知道如何将其循环回菜单并使其跟踪捐赠和投资
  • 你永远不会在代码中自己调用main。绝不。从来没有例外。你不要自己打电话给main。重新阅读关于循环语句主题的课堂笔记或教程或书籍。

标签: c


【解决方案1】:

您应该将要重复的部分放入循环中,使用变量来决定何时停止。

这里是一个例子,同样在开关结构中你应该使用默认选项,也许是为了警告用户他们插入的选项无效。

int main() {
  // variables
  int donation, investment, fund, total, tdonations, tinvestments;

  // prompt user
  printf("Welcome!\n");
  printf("What is the initial balance of the fund\n");
  scanf("%d", &fund);
  
  //loop until the user decide so
  int exit = 0;
  while(exit == 0){
      int ans;

      printf("What would you like to do?\n");
      printf("\t1-  Make a Donation\n");
      printf("\t2-  Make an investment\n");
      printf("\t3-  Print balance of fund\n");
      printf("\t4- Quit\n");
      scanf("%d", &ans);

      //switch
      switch (ans) {
          case 1:
             printf("How much would you like to donate?\n");
             scanf("%d", &donation);
             break;

          case 2:
             printf("How much would you like to invest\n");
             scanf("%d", &investment);
             break;

          case 3:
             total =  donation + fund - investment;
             if (total < fund) {
                 printf("You cannot make an investment of that amount\n");
             }
             else {

              printf("The current balance is %d\n", total);
              printf(" There have been %d donations and %d investments.\n", tdonations, tinvestments);

             }
             break;

          case 4:
             exit = 1;
             break;

          default:
             printf("Incorrect option\n");
             break;
      }


   }

   return 0;

  }

【讨论】:

  • 如何添加一个跟踪捐赠和投资总数的计数器
  • 您创建一个变量并将其初始化为零,@TheOnion。每次用户进行投资或捐赠时,您都会增加。
猜你喜欢
  • 2015-10-16
  • 2021-10-25
  • 1970-01-01
  • 2013-07-20
  • 2012-02-26
  • 2021-05-29
  • 2012-01-22
  • 2014-05-16
  • 1970-01-01
相关资源
最近更新 更多