i have created a bank program with a do while loop switch statement and functions. everything works except when i choose to withdraw it isnt subtracting instead its giving me my account balance without taking out the withdrawal can anyone suggest why?
Code:
#include <stdio.h>
char displaymenu();
float getdeposit(float balance);
float getwithdrawal(float balance);
void displaybalance(float );
int main()
{
char choice;
char transaction='y';
float deposit=0;
float withdrawal;
float balance;
do {
choice=displaymenu();
switch (choice)
{
case 'd':case 'D':
balance=getdeposit(balance);
break;
case 'w':case 'W':
withdrawal=getwithdrawal(balance);
break;
case 'b':case 'B':
printf("Checking your account balance\n");
displaybalance(balance);
break;
case 'q':case 'Q':
printf("Quit\n");
printf("Thankyou");
break;
default:
printf("Invalid Choice\n");
}
printf("\n\n\n DO U WISH TO HAVE ANOTHER TRANSCATION?(y/n): \n");
scanf(" %c",&transaction);
}while
(transaction == 'y'|| transaction == 'Y');
return 0;
}
char displaymenu()
{
char choice;
printf("Welcome to Federal 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 balance\n");
printf("q. Quit\n");
scanf ("\n%c",&choice);
return choice;
}
float getdeposit(float balance)
{
float deposit;
printf("How much would you like to deposit?\n");
scanf ("%f",&deposit);
balance=deposit+balance;
return balance;
}
float getwithdrawal(float balance)
{
float withdrawal;
printf("How much would you like to withdraw?\n");
scanf ("%f",&withdrawal);
balance=balance-withdrawal;
return balance;
}
void displaybalance(float balance)
{
printf("Your balance is %.2f\n",balance);
}