Hi everyone, this is my 2nd post. My first post I was working on a banking program, well unfortunately I am still working on it. This is my final output on the bottom. It works fine I like it, but I want to simplify it with the use of functions. Primarily I just want to convert these 3 blocks of code below into functions and arrange it so the program will run smoothly. Any input would be greatly appreciated.
1.
Code:
do
{
printf ("Now enter your current balance in dollars and cents: ");
scanf ("%f", &starting_balance);
fflush(stdin);
if (starting_balance < 0)
printf("Error: Starting Balance must be at least zero!\n\n");
} while (starting_balance < 0);
2.
Code:
do
{
printf ("\nEnter the number of withdrawals: ");
scanf ("%i", &number_of_withdrawals);
fflush(stdin);
if (number_of_withdrawals > 50)
printf ("*** Too many withdrawals. ***\n");
if (number_of_withdrawals < 0)
printf ("*** Negative number of withdrawals not allowed. ***\n");
} while (number_of_withdrawals > 50 || number_of_withdrawals < 0);
3.
Code:
do
{
printf ("\n\nEnter the number of deposits: ");
scanf ("%i", &number_of_deposits);
fflush(stdin);
if (number_of_deposits > 50)
printf ("*** Too many deposits. ***\n");
if (number_of_deposits < 0)
printf ("*** Negative number of deposits not allowed. ***\n");
} while ( number_of_deposits > 50 || number_of_deposits < 0);
Code:
#include <stdio.h>
void main (void)
{
/* Variable Declarations */
/* --------------------- */
int number_of_deposits, number_of_withdrawals, i;
float amount_to_deposit[50], amount_to_withdraw[50];
float starting_balance, current_balance;
char name[30];
/* Display an introduction header */
/* ------------------------------ */
printf ("Welcome to the Sears Bank Balancing System\n\n");
printf("Please enter your first name: ");
gets(name); /* or use scanf("%s", name); */
fflush(stdin);
printf("\nHello %s\n\n", name);
/* Query the user for the starting balance (starting_balance >= 0) */
/* ------------------------------------------------------------ */
do
{
printf ("Now enter your current balance in dollars and cents: ");
scanf ("%f", &starting_balance);
fflush(stdin);
if (starting_balance < 0)
printf("Error: Starting Balance must be at least zero!\n\n");
} while (starting_balance < 0);
/* Prompt for the number of withdrawals */
/* Ensure that user did not enter more than 50 withdrawals. */
/* -------------------------------------------------------- */
do
{
printf ("\nEnter the number of withdrawals: ");
scanf ("%i", &number_of_withdrawals);
fflush(stdin);
if (number_of_withdrawals > 50)
printf ("*** Too many withdrawals. ***\n");
if (number_of_withdrawals < 0)
printf ("*** Negative number of withdrawals not allowed. ***\n");
} while (number_of_withdrawals > 50 || number_of_withdrawals < 0);
/* Prompt for the number of deposits. */
/* Ensure that user did not enter more than 50 deposits. */
/* ----------------------------------------------------- */
do
{
printf ("\n\nEnter the number of deposits: ");
scanf ("%i", &number_of_deposits);
fflush(stdin);
if (number_of_deposits > 50)
printf ("*** Too many deposits. ***\n");
if (number_of_deposits < 0)
printf ("*** Negative number of deposits not allowed. ***\n");
} while ( number_of_deposits > 50 || number_of_deposits < 0);
/* Retain starting balance for bank record. */
/* ---------------------------------------- */
current_balance = starting_balance;
/* Prompt for all deposit amounts, keeping track of current balance. */
/* ---------------------------------------------------------------- */
printf ("\n"); /* spacing */
for (i = 0; i < number_of_deposits; i++ )
{
do
{
printf ("Enter the amount of deposit #%i: ", i+1 );
scanf ("%f", &amount_to_deposit[i]);
fflush(stdin);
if (amount_to_deposit[i] <= 0)
printf ("*** Deposit amount must be positive! Please re-enter ***\n");
} while (amount_to_deposit[i] <= 0);
current_balance = current_balance + amount_to_deposit[i];
} /* end for loop */
/* Prompt for all withdrawal amounts, keeping track of current balance. */
/* ------------------------------------------------------------------- */
printf ("\n"); /* spacing */
for (i = 0; i < number_of_withdrawals; i++ )
{
/* Enter amounts, and be sure amount does not exceed current balance. */
/* ------------------------------------------------------------------ */
do
{
printf ("Enter the amount of withdrawal #%i: ", i+1 );
scanf ("%f", &amount_to_withdraw[i]);
fflush(stdin);
if (amount_to_withdraw[i] > current_balance)
printf ("*** Withdrawal amount exceeds current balance. ***\n");
else
if (amount_to_withdraw[i] <= 0)
printf ("*** Withdrawal amount must be greater than zero! ***\n");
} while (amount_to_withdraw[i] > current_balance || amount_to_withdraw[i] <= 0);
current_balance = current_balance - amount_to_withdraw[i];
/* If Balance goes to zero, break the loop, no more withdrawals allowed, reset the
withdrawal count so the bank record prints properly! */
if(current_balance == 0)
{
printf ("\n*** Balance is now zero. No more withdrawals allowed! ***\n");
number_of_withdrawals = i + 1;
break;
} /* end-if */
} /* end for loop.*/
printf ("\n"); /* spacing */
/* Output closing balance and appropriate closing message. */
/* ------------------------------------------------------- */
printf ("*** The closing balance %s is $%.2f ***\n\n", name, current_balance);
if (current_balance >= 50000.00 )
printf ("*** %s time to invest some money! ***", name);
else
if (current_balance >= 15000.00 )
printf ("*** %s maybe you should consider a CD. ***", name);
else
if (current_balance >= 1000.00 )
printf ("*** %s keep up the good work. ***", name);
else
printf ("*** %s your balance is very low. ***", name);
/* Output bank record: Starting balance, deposits and withdrawals. */
/* ------------------------------------------------------------- */
printf ("\n\n"); /* spacing */
printf (" *** Bank Record ***\n");
printf ("Starting Balance: $%13.2f\n\n", starting_balance);
for (i = 0; i < number_of_deposits; i++ )
printf ("Deposit #%2i: %15.2f\n", i+1, amount_to_deposit[i]);
if (number_of_deposits > 0)
printf ("\n"); /* spacing */
for (i = 0; i < number_of_withdrawals; ++i )
printf ("Withdrawal #%2i: %15.2f\n", i+1, amount_to_withdraw[i]);
printf ("\nEnding Balance: $%15.2f\n\n", current_balance);
getchar();
} /* end main. */