这是一个标准的银行帐户计划。允许存款,取款和查看资金。我无法让程序进入我的switch语句中的函数,这是基于他们的选择。这是此代码的输出。 CodeOutput。我不是要求任何人为我编码,如果你可以指出我出错的地方。
#include <stdio.h>
float getDeposit(float currentBalance);
float getWithdrawal(float currentBalance);
float displayBalance(float currentBalance);
char displayMenu();
int main()
{
float currentBalance=200,newBalanceDep,newBalanceWith;
char choice;
choice = displayMenu();
switch (choice)
{
case 'D': case 'd':
newBalanceDep=getDeposit(currentBalance);
break;
case 'W': case 'w':
newBalanceWith=getWithdrawal(currentBalance);
break;
case 'B': case 'b':
displayBalance(currentBalance);
break;
case 'Q': case 'q':
printf("Thank you!");
break;
default:
printf("Invalid choice.");
break;
}
return 0;
}
char displayMenu()
{
char choice;
printf("Welcome to HFC Credit Union! \n");
printf("Please select from the following menu: \n");
printf("D: Make a deposit \n");
printf("W: Make a withdrawal \n");
printf("B: Check your account balance \n");
printf("Q: To quit \n");
scanf("\n%c",choice);
return choice;
}
float getDeposit(float currentBalance)
{
float depositAmount;
float newBalanceDep;
printf("Enter amount you would like to deposit: /n");
scanf("%f",&depositAmount);
if(depositAmount>0)
{
newBalanceDep=depositAmount+currentBalance;
}
return newBalanceDep;
}
float getWithdrawal(float currentBalance)
{
float withdrawalAmount;
float newBalanceWith;
printf("Enter amount you would like to withdrawal: /n");
scanf("%f",&withdrawalAmount);
if(withdrawalAmount>currentBalance)
{
printf("Insufficient Funds. Try again.");
printf("Enter amount you would like to withdrawal: /n");
scanf("%f",&withdrawalAmount);
}
else if(withdrawalAmount<=currentBalance)
{
newBalanceWith=withdrawalAmount+currentBalance;
}
return newBalanceWith;
}
float displayBalance(float currentBalance)
{
printf("Your current balance is %.2f",currentBalance);
}
答案 0 :(得分:2)
您需要将&choice
传递给scanf
,而不是choice
。
char choice; /*...*/; scanf("\n%c",choice); // --->
// v---- add
char choice; /*...*/; scanf("\n%c",&choice);`
传递choice
是未定义的行为。
一个好的编译器应该给你一个警告。