I need to add two trivial functions to the following code. I am having an issue with what i may need to change in the current code to meet the requirements. I am already displaying the bank record but now using a function
Here are some examples of what i can add to the code
1. Obtain opening (current) balance.
2. Obtain number the number of deposits.
3. Obtain number of withdrawals.
4. Obtain deposit amounts.
5. Obtain withdrawal amounts.
Output functions:
1. Display closing balance and associated message.
2. Display message based on closing balance.
3. Display bank record.
#include <stdio.h>
void main (void)
{
/* Declare variables */
/*-------------------*/
int num_deposit = 0, i, s;
int num_withdrawal = 0;
float current_balance;
float ending_balance;
float total_deposit = 0;
float deposit[5];
float withdrawals[5];
/* Intro and greeting to user. Allows starts the input sequence for
the user */
/*---------------------------------------------------------------------------*/
printf("Welcome to the Computer Banking System \n\n");
printf("Enter your current balance in dollars and cents: ");
scanf("%f", ¤t_balance);
while(current_balance < 0)
{
printf("*** Beginning balance must be at least zero, please re-enter!");
printf("Now enter current balance in dollars and cents: ");
scanf("%f", ¤t_balance);
}
printf("\nEnter the number of deposits: ");
scanf("%d", &num_deposit);
while(num_deposit < 0||num_deposit>5)
{
printf("\n*** Invalid number of deposits must be at least zero,");
printf("please re-enter.\n");
printf("Enter the number of deposits: ");
scanf("%d", &num_deposit);
}
printf("\nEnter the number of withdrawals: ");
scanf("%d", &num_withdrawal);
while(num_withdrawal < 0||num_withdrawal>5)
{
printf("*** Invalid number of withdrawals must be at least zero,please re-enter.\n");
printf("Enter the number of withdrawals: ");
scanf("%d", &num_withdrawal);
}
for(i = 0; i < num_deposit; i++)
{
printf("\nEnter the amount of deposit #%d: ", i+1);
scanf("%f", &deposit[i]);
while(deposit[i]<0)
{
printf("*** deposit amount must be at least zero\n");
printf("please re-enter.\n");
printf("\nEnter the amount of deposit #%d: ", i+1);
scanf("%f", &deposit[i]);
}
total_deposit+=deposit[i];
}
for(i = 0; i < num_withdrawal; i++)
{
printf("\nEnter the amount of withdrawal #%d: ", i+1);
scanf("%f", &withdrawals[i]);
while(withdrawals[i]<0)
{
printf("*** withdrawal amount must be at least zero\n");
printf("please re-enter.\n");
printf("\nEnter the amount of withdrawal #%d: ", i+1);
scanf("%f", &withdrawals[i]);
}
if(withdrawals[i]>(current_balance+total_deposit))
{
printf("*** Withdrawal amount exceeds current balance.
***\n");
}
else
total_deposit -=withdrawals[i];
/* gives user their total, along with some financial advice or
statements */
/*------------------------------------------------------------------------*/
}
ending_balance= current_balance+total_deposit;
printf("The closing balance is %.2f\n",ending_balance);
if(ending_balance>=50000)
printf("*** it is time to invest some money***\n");
else if(ending_balance>=15000)
printf("*** maybe you should consider a CD. ***\n");
else if(ending_balance>=1000)
printf("*** keep up the good work. ***\n");
else if(ending_balance>=0)
printf("*** your balance is getting low. ***\n");
/* Provides a bank record or statement. Providing totals*/
/*------------------------------------------------------*/
printf("***** Bank Record ******\n");
printf("Starting Balance $%.2f\n\n",current_balance);
for(i = 0; i < num_deposit; i++)
{
printf("Deposit #%d : $%.2f\n",i+1,deposit[i]);
}
printf("\n");
for(i = 0; i < num_withdrawal; i++)
{
printf("Withdrawal #%d : $%.2f\n",i+1,withdrawals[i]);
}
printf("\n");
printf("Ending balance :$%.2f\n",ending_balance);
printf("\n");
}
//end main
// C program to implement Computer Banking System
#include <stdio.h>
// input functions
float getCurrentBalance();
int getNumberOfDeposits();
int getNumberOfWithdrawals();
float getDepositAmount(int i);
float getWithdrawalAmount(int i,float balance);
// output functions
void displayClosingBalanceAndMessage(float balance);
void displayBankRecord(float startingBalance, float currentBalance,
int numOfDeposits, int numOfWithdrawals, float deposits[], float
withdrawals[]);
void main(void) {
/* Declare variables */
/*-------------------*/
int x, numberOfDeposits, numberOfWithdrawals;
float deposits[5], withdrawals[5];
float currentBalance, startingBalance;;
float totalDeposits = 0, totalWithdrawals = 0;
/* Intro and greeting to user. Allows starts the
input sequence for the user */
/*---------------------------------------------------------------------------*/
printf("Welcome to the Computer Banking
System.\n\n");
currentBalance= getCurrentBalance();
startingBalance = currentBalance;
numberOfDeposits = getNumberOfDeposits();
numberOfWithdrawals = getNumberOfWithdrawals();
printf("\n");
/* add the deposits entered */
for (x = 1; x <= numberOfDeposits; x++)
{
deposits[x-1] =
getDepositAmount(x);
/* sum of total deposits */
totalDeposits = totalDeposits +
deposits[x-1];
}
currentBalance = currentBalance + totalDeposits;
printf("\n");
/* number of withdrawals */
for (x = 1; x <= numberOfWithdrawals; x++)
{
/* total withdrawal sum */
withdrawals[x-1] =
getWithdrawalAmount(x,currentBalance);
totalWithdrawals = totalWithdrawals
+ withdrawals[x-1];
/* balance calculation */
currentBalance = currentBalance -
withdrawals[x-1];
if (currentBalance == 0.00f)
{
/* zero
balance error message */
printf ("\n ***
Balance is now zero. No more withdrawals can be made at this time.
***\n");
numberOfWithdrawals = x;
break;
}
}
displayClosingBalanceAndMessage(currentBalance);
displayBankRecord(startingBalance,currentBalance,numberOfDeposits,numberOfWithdrawals,
deposits,withdrawals);
}
// function to get and return the current balance from
user
float getCurrentBalance()
{
float balance;
do{
/* prompts the user to enter
current balance */
printf ("Enter your current balance
in dollars and cents: ");
scanf ("%f",&balance);
/* error message if current
balance less than 0 */
if (balance < 0)
printf ("***
Beginning balance must be at least zero, please
re-enter.\n");
}
/* this will end the loop if current balance is less
than 0 */
while (balance < 0);
return balance;
}
// function to get and return the number of deposits from the
user
int getNumberOfDeposits()
{
int numOfDeposits;
do
{
/* prompts for user to enter
number of deposits */
printf ("\nEnter the number of
deposits (0 - 5): ");
scanf
("%d",&numOfDeposits);
/* a loop starts limiting the
user on number of deposits */
if (numOfDeposits < 0 ||
numOfDeposits > 5)
printf ("***
Invalid number of deposits, please re-enter.\n");
}
/* if amount of deposits is between 0 and 5 loop will
terminate */
while (numOfDeposits < 0 || numOfDeposits >
5);
return numOfDeposits;
}
// function to get and return the number of withdrawals from the
user
int getNumberOfWithdrawals()
{
int numOfWithdrawals;
do
{
/* prompts user to enter number
of withdrawals */
printf ("\nEnter the number of
withdrawals (0 - 5): ");
scanf
("%d",&numOfWithdrawals);
/* error message if amount is less
than 0 or over 5 */
if (numOfWithdrawals < 0 ||
numOfWithdrawals > 5)
printf ("***
Invalid number of withdrawals, please re-enter.\n");
}
/* loop will terminate if amount is between 0 and 5
*/
while (numOfWithdrawals < 0 || numOfWithdrawals
> 5);
return numOfWithdrawals;
}
// function to get and return xth deposit amount from user
float getDepositAmount(int x)
{
float amount;
do
{
/* prompt to enter amount of each
deposit */
printf ("Enter the amount of
deposit #%d: ", x);
scanf ("%f",&amount);
/* loop continues until amount
is 0 or greater */
if (amount < 0)
printf ("***
Deposit amount must be at least zero, please re-enter.\n");
}
while (amount < 0);
return amount;
}
// function to get and return xth withdrawal amount from
user
float getWithdrawalAmount(int x, float balance)
{
float amount;
do
{
/* prompt for user to enter amount
of each withdrawal */
printf ("Enter the amount of
withdrawal #%i: ", x);
scanf ("%f",&amount);
/* error message if withdrawal
exceeds current balance */
if (amount > balance)
printf ("***
Withdrawal amount exceeds current balance.\n");
else
/* error message
if amount is less than 0 */
if (amount <
0)
printf ("*** Withdrawal amount must be at least
zero, please re-enter.\n");
}
/* loop ends when amount greater than 0 */
while (amount > balance || amount < 0);
return amount;
}
// function to display the closing balance and message depending
on the closing balance
void displayClosingBalanceAndMessage(float balance)
{
/* various final display messages */
printf ("\n*** The closing balance is $%.2f *** \n",
balance);
if (balance >= 50000.00)
printf ("*** It is time to invest
some money! *** \n\n");
else if (balance >= 15000.00)
printf ("*** Maybe you should
consider a CD. *** \n\n");
else if (balance >= 1000.00)
printf ("*** Keep up the good work!
*** \n\n");
else
printf ("*** Your balance is
getting low! *** \n\n");
}
// function to display all the details of the bank account, the
starting balance, the transactions (deposit and withdrawal) and
ending balance
void displayBankRecord(float startingBalance, float currentBalance,
int numOfDeposits, int numOfWithdrawals, float deposits[], float
withdrawals[])
{
int x;
/* bank record */
printf (" *** Bank Record ***\n");
printf ("\nStarting Balance: $%.2f\n\n",
startingBalance);
for (x = 1; x <= numOfDeposits; x++)
{
printf ("Deposit #%x: %.2f\n", x,
deposits[x-1]);
}
printf("\n");
for ( x = 1; x<= numOfWithdrawals; x++)
{
printf ("Withdrawal #%i: %.2f\n",
x, withdrawals[x-1]);
}
printf ("\nEnding Balance: $%.2f\n\n",
currentBalance);
}
//end of program
Get Answers For Free
Most questions answered within 1 hours.