Question
Hello. I need help with a c program. The withdrawal function is not working. It is not using the current balance of the bank account.
Hello. I need help with a c program. The withdrawal function is not working. It is not using the current balance of the bank account. At first it worked several times, but then it did not even after I did not make any changes to the code. Can you please check out my code and let me know what I need to do to get the withdrawal function to work? When I enter any withdrawal amount it states that the withdrawal is more than the current balance, please re-enter. Please see code below:
#include
#define MAX 5
/*Function for initial greeting*/
/*-----------------------------*/
void output_initial_greeting(void)
{
printf ("Welcome to the Computer Banking System ");
} /*end function*/
/*Function to obtain beginning bank balance*/
/*-----------------------------------------*/
float get_beginning_balance(void)
{
float beginning_bank_bal;
char c;
do
{
printf ("Enter your current balance in dollars and cents: ");
scanf ("%f", &beginning_bank_bal);
while ( (c = getchar() != ' ') && c != EOF); /* clear input buffer */
if (beginning_bank_bal < 0)
printf("*** Beginning balance must be at least zero, please re-enter. ");
} while (beginning_bank_bal < 0);
return beginning_bank_bal;
}/*end function*/
/*Function to obtain deposit amounts*/
/*----------------------------------*/
float get_deposit_amount(int x)
{
float deposit;
char c;
do
{
printf ("Enter the amount of deposit #%i: ", x + 1 );
scanf ("%f", &deposit);
while ( (c = getchar() != ' ') && c != EOF); /*clear input buffer*/
if (deposit <= 0)
printf ("*** deposit amount must be greater than zero, please re-enter. ");
} while (deposit <= 0);
}/*end function*/
/*Function to obtain withdrawal amounts*/
/*-------------------------------------*/
float get_withdrawal_amount(int x)
{
float withdrawal;
float closing_balance;
char c;
do
{
printf ("Enter the amount of withdrawal #%i: ", x + 1);
scanf ("%f", &withdrawal);
while ( (c = getchar() != ' ') && c != EOF); /*clear input buffer*/
if (withdrawal > closing_balance)
printf ("*** withdrawal amount exceeds current balance. ");
else
if (withdrawal <= 0)
printf ("*** withdrawal amount must be greater than zero, please re-enter. *** ");
} while (withdrawal > closing_balance || withdrawal <= 0);
}/*end function*/
int main (void)
{
/* Variable declarations */
/*----------------------*/
int num_deposits, num_withdrawals, i;
float deposit[MAX], withdrawal[MAX];
float beginning_bank_balance, closing_balance, running_balance;
char c;
/* Output initial greeting*/
/*------------------------*/
output_initial_greeting();
/* Prompt user to enter current bank balance */
/*-------------------------------------------*/
beginning_bank_balance = get_beginning_balance();
do
{
printf (" Enter the number of deposits: ");
scanf ("%i", &num_deposits);
while ( (c = getchar() != ' ') && c != EOF);
if (num_deposits > MAX || num_deposits < 0)
printf ("*** Invalid number of deposits, please re-enter. ", MAX);
} while ( num_deposits >MAX || num_deposits < 0);
do
{
printf (" Enter the number of withdrawals: ");
scanf ("%i", &num_withdrawals);
while ( (c = getchar() != ' ') && c != EOF);
if (num_withdrawals > MAX || num_withdrawals < 0)
printf ("*** Invalid number of withdrawals, please re-enter. ");
} while (num_withdrawals > MAX || num_withdrawals < 0);
closing_balance = beginning_bank_balance;
printf (" ");
for (i = 0; i < num_deposits; i++ )
{
/* Trap loop for deposit amounts*/
/*------------------------------*/
deposit[i] = get_deposit_amount(i);
closing_balance = closing_balance + deposit[i]; /*increment balance*/
}
printf (" ");
for (i = 0; i < num_withdrawals; i++ )
{
/* Trap loop for withdrawal amounts*/
/*---------------------------------*/
withdrawal[i] = get_withdrawal_amount(i);
closing_balance = closing_balance - withdrawal[i]; /*decrement balance*/
if(closing_balance == 0)
{
printf (" *** Balance is now zero. No more withdrawals allowed! *** ");
withdrawal[i] = i + 1;
break;
}
}
printf (" ");
/*Output statement to print depending on bank closing balance */
/*------------------------------------------------------------*/
printf ("*** The closing balance is $%.2f *** ", closing_balance);
if (closing_balance >= 50000.00 )
printf ("*** It is time to invest some money! ***");
else
if (closing_balance >= 15000.00 )
printf ("*** Maybe you should consider a CD. ***");
else
if (closing_balance >= 1000.00 )
printf ("*** Keep up the good work. ***");
else
printf ("*** Your balance is getting low! ***");
/* This will print report of bank balances and transactions */
/*----------------------------------------------------------*/
printf (" ");
printf (" *** Bank Record *** ");
printf ("Starting Balance: $%13.2f ", beginning_bank_balance);
for (i = 0; i < num_deposits; i++ )
printf ("deposit #%i: %15.2f ", i+1, deposit[i]);
if (num_deposits > 0)
printf (" ");
for (i = 0; i < num_withdrawals; ++i )
printf ("withdrawal #%i: %15.2f ", i+1, withdrawal[i]);
printf (" Ending Balance: $%15.2f ", closing_balance);
getchar();
return 0;
} /*end main*/
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started