(コードは 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;
}
したがって、これを初めて実行すると、次のように出力されます。
年の初めに、ファンドにはいくらのお金が入っていますか? 1000 (整数を入力) なぜこれが印刷されるのですか (これはデフォルトのセクションにあり、印刷すべきではありません) どうしますか? 1 - 寄付をする 2 - 投資をする 3 - 基金の残高を印刷する 4 - やめる
これを修正するにはどうすればよいですか?