Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 默认情况下,开关在启动时运行_C_Switch Statement_Default - Fatal编程技术网

C 默认情况下,开关在启动时运行

C 默认情况下,开关在启动时运行,c,switch-statement,default,C,Switch Statement,Default,(代码用C表示) 出于某种原因,我的代码总是在打印菜单之前执行“默认”部分中列出的内容 #include <stdio.h> int main(){ double funds=0; //initial funds double donation=0; //donation to funds double investment=0; //amount invested int choice=0; //for the switch int countdonate=0; //co

(代码用C表示)

出于某种原因,我的代码总是在打印菜单之前执行“默认”部分中列出的内容

#include <stdio.h>

int main(){    
double funds=0; //initial funds
double donation=0; //donation to funds
double investment=0; //amount invested
int choice=0; //for the switch
int countdonate=0; //counts number of donations
int countinvest=0; //counts number of investments

printf("How much money is in the fund at the start of the year? \n");
scanf("%lf", &funds);//sets initial funds


while (choice!=4)

{

    switch (choice)

    { case 1: //option for donating
        printf("How much would you like to donate? \n");
        scanf("%lf", &donation);
        funds=funds+donation;
        countdonate++;
        break;

     case 2: //option for investing
        printf("How much would you life to invest? \n");
        scanf("%lf", &investment);
        if (investment>funds){
            printf("You cannot make an investment of that amount \n");
            break;}
        else{
            funds=funds-investment;
            countinvest++;
            break;}

     case 3: //prints current balance, number of donations, and number of investments
        printf("The current balance is %.2lf \n", funds);
        printf("There were %d donation(s) and %d investment(s) \n", countdonate, countinvest);
        break;


     default: //if the user selections something that isnt listed
            printf("why is this printing \n");
            break;
    }


    //displays list of options
    printf("What would you like to do? \n1 - Make a donation \n2 - Make an investment \n3 - Print balance of fund \n4 - Quit \n");
    scanf("%d", &choice);

    //quit option, outside of the loop.
    //prints current balance, number of donations, number of investments, and ends the program
    if (choice==4)
       {printf("The current balance is %.2lf \n", funds);
        printf("There were %d donation(s) and %d investment(s) \n", countdonate, countinvest);
       }

}

return 0;
}
#包括
int main(){
双基金=0;//初始基金
双重捐赠=0;//捐赠给基金
双重投资=0;//投资金额
int choice=0;//用于开关
int countDevate=0;//统计捐赠数量
int countinvest=0;//计算投资数量
printf(“年初基金中有多少资金?\n”);
scanf(“%lf”,&funds);//设置初始资金
while(选项!=4)
{
开关(选择)
{案例1://捐赠选择
printf(“您想捐赠多少?\n”);
scanf(“%lf”和捐赠);
资金=资金+捐赠;
计数++;
打破
案例2://投资选择权
printf(“您愿意投资多少生命?\n”);
scanf(“%lf”和“投资”);
if(投资>基金){
printf(“您不能进行该金额的投资”);
中断;}
否则{
资金=资金投资;
countinvest++;
中断;}
案例3://打印当前余额、捐赠数量和投资数量
printf(“当前余额为%.2lf\n”,资金);
printf(“共有%d项捐赠和%d项投资)\n”,countinvest,countinvest);
打破
默认值://如果用户选择了未列出的内容
printf(“为什么要打印\n”);
打破
}
//显示选项列表
printf(“你想做什么?\n1-捐款\n2-投资\n3-打印基金余额\n4-退出\n”);
scanf(“%d”,选择(&C);
//退出选项,在循环之外。
//打印当前余额、捐赠数量、投资数量,并结束计划
如果(选项==4)
{printf(“当前余额为%.2lf\n”,资金);
printf(“共有%d项捐赠和%d项投资)\n”,countinvest,countinvest);
}
}
返回0;
}
所以当我第一次运行它时,它会打印:


年初基金里有多少钱? 1000(我输入一个整数) 为什么要打印(这在默认部分,不应该打印) 你想做什么? 1-捐款 2-进行投资 3-打印基金余额 4-退出



如何解决此问题?

初始选择值为0,因此switch语句正在打印您不期望的内容。要解决此问题,您应该移动下面的开关,如图所示。在if(选项==4)中也添加打断符


在循环之前,choice==0,并且您不读入任何内容来重置它,因此switch语句转到默认情况。
//quit option, outside of the loop.
//prints current balance, number of donations, number of investments, and ends the program
if (choice==4)
   {printf("The current balance is %.2lf \n", funds);
    printf("There were %d donation(s) and %d investment(s) \n", countdonate, countinvest);
    break;
   }

switch (choice)
...