我正在尝试运行以下程序..但它一直在给我错误结束..似乎我忘了关闭一个循环左右..如果有人能够帮助我那会太棒了!!
#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 :(得分:2)
您的第一个else
没有if
。这一个
//Quit for selection 4
else if (user_selection = 4) {
另外,正如@Yu Hao在评论中指出的那样,C ++中的等式比较运算符是==
,而不是=
。
前面for
周期背后的想法也逃脱了我。你真的明白for
语句的作用吗?我强烈怀疑你没有。
如果您在}
之前添加else
,则应该在代码中正确平衡if
和else
,但它仍然不会那些for
做了一些有意义的事。
答案 1 :(得分: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++) {
使循环完全无用,即它就像没有循环一样,只是另一个范围:
{
答案 2 :(得分:0)
你错过了if if之前的if和“Quit for selection 4”评论。
答案 3 :(得分:0)
使用switch
案例代替用户选择
switch(user_selection)
{
case 1:
//do something i.e. make donation
case 2:
//do something
.
.
.
default:
}
编辑:请尝试以下代码。我添加的唯一内容是}
您错过了'&amp;'在%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;
}