【发布时间】:2021-01-07 01:53:34
【问题描述】:
问题:奖学金捐赠基金第 2 部分 (fund2.c)
我如何返回主菜单并添加一个计数器来计算捐赠和投资的总数,这就是我得到的?
然后,您的程序应该允许用户使用以下选项:
- 捐款
- 投资
- 打印资金余额
- 退出
#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