Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <stdio.h>

/* Variable declarations */
/* initial balance */
float promptInitialBalance(void);

/*get intended number of deposits */
int promptIntendedNumDeposits(void);

/*get intended number of withdrawals */
int promptIntendedNumWithdrawals(void);

/*accept account deposits*/
float promptDeposits(float balance,int numDeposits, float depositRecords[5]);

/* accept accont withdrawals */
float promptWithdrawals(float balance, int numWithdrawals, float withdrawalRecords[5]);

/* display summary of closing account balance */
void printClosingBalanceSummary(float balance);

// displays bank record */
void printBankRecord(float balance_0, float balance, int numDeposits, int numWithdrawals, float depositRecords[5], float withdrawalRecords[5]);

/* start main */
int main(void)
{
    float balance_0 = 0.0;     /* starting balance */
    float balance = 0.0;        /* total balance */
    int numDeposits=0;       /* number of deposits */
    int numWithdrawals=0;   /* number of withdrawals */
    float depositRecords[5];     /* record of valid deposit amounts */
    float withdrawalRecords[5];  /* record of valid withdrawal amounts */
    
    
    printf("Welcome to the Computer Banking System\n");
    
    balance_0 = promptInitialBalance();
    
    /* updated total balance with starting balance */
    balance = balance_0;
    
    numDeposits = promptInitialBalance();
    
    numWithdrawals = promptIntendedNumWithdrawals();
    
    printf("\n");
    
    balance = promptDeposits(balance, numDeposits, depositRecords);
    
    printf("\n");
    
    balance = promptWithdrawals(balance, numWithdrawals, withdrawalRecords);
    
    printClosingBalanceSummary(balance);
    
    printBankRecord(balance_0, balance, numDeposits, numWithdrawals, depositRecords, withdrawalRecords);
    
    return 0;
}

/* Function Definitions*/
float promptInitialBalance(void)
{
    float balance_0 = 0.0;     /* starting balance */
    
    /* continuously prompt user for a starting account balance*/
    while (balance_0 < 0.0)
    {
        printf("\nEnter your current balance in dollars and cents: ");
        scanf("%lf", &balance_0);
        
        if (balance_0 >= 0.0)
            break;
        else
            printf("*** Beginning balance must be at least zero, please re-enter.\n");
    }
    
    return balance_0;
}

int promptIntendedNumDeposits(void)
{
    int numDeposits = 0;         // number of deposits
    
    /* continuously prompt user for the number of intended deposits */
    while (numDeposits < 0 || numDeposits > 5)
    {
        printf("\nEnter the number of deposits (0 - 5): ");
        scanf("%d", &numDeposits);
        
        if (numDeposits >= 0 && numDeposits <= 5)
            break;
        else
            printf("*** Invalid number of deposits, please re-enter.\n");
    }
    
    return numDeposits;
}

int promptIntendedNumWithdrawals(void)
{
    int numWithdrawals = 0;      /*number of withdrawals*/
    
    /*continuously prompt user for the number of intended withdrawals*/
    
    while (numWithdrawals < 0 || numWithdrawals > 5)
    {
        printf("\nEnter the number of withdrawals (0 - 5): ");
        scanf("%d", &numWithdrawals);
        
        if (numWithdrawals >= 0 && numWithdrawals <= 5)
            break;
        else
            printf("*** Invalid number of withdrawals, please re-enter.\n");
    }
    
    return numWithdrawals;
}

float promptDeposits(float balance, int numDeposits, float depositRecords[5])
{
    /* prompt the user to provide a deposit amount until we have enough valid amounds */
    int depositCount = 0;
    float depositAmount = 0.0;
    while (depositCount < numDeposits)
    {
        printf("Enter the amount of deposit #%d: ", depositCount + 1);
        scanf("%lf", &depositAmount);
        
        if (depositAmount >= 0.0)
        {
            depositRecords[depositCount] = depositAmount;
            balance += depositAmount;
            depositCount++;
        }
        else
            printf("*** Deposit amount must be greater than or equal to zero, please try again.\n\n");
    }
    
    return balance;
}

float promptWithdrawals(float balance, int numWithdrawals, float withdrawalRecords[5])
{
    /* prompt the user to provide a withdrawal amount until we have enough valid amounts */
    int withdrawalCount = 0;
    float withdrawalAmount = 0.0;
    while (withdrawalCount < numWithdrawals)
    {
        printf("Enter the amount of withdrawal #%d: ", withdrawalCount + 1);
        scanf("%lf", &withdrawalAmount);
        
        if (withdrawalAmount >= 0.0)
        {
            if ((balance - withdrawalAmount) < 0.0)
            {
                printf("*** Withdrawal amount exceeds current balance.\n\n");
            }
            else
            {
                withdrawalRecords[withdrawalCount] = withdrawalAmount;
                balance -= withdrawalAmount;
                withdrawalCount++;
            }
        }
        else
            printf("*** Withdrawal amount must be greater than or equal to zero, please try again.\n\n");
    }
    
    return balance;
}

void printClosingBalanceSummary(float balance)
{
    printf("\n*** The closing balance is: $%.2f ***\n", balance);
    
    if (balance >= 50000.00)
    {
        printf("*** It is time to invest some money! ***\n");
    }
    else if (balance >= 15000.00 && balance <= 49999.9)
    {
        printf("*** Maybe you should consider a CD. ***\n");
    }
    else if (balance >= 1000.00 && balance <= 14999.99)
    {
        printf("*** Keep up the good work! ***\n");
    }
    else if (balance >= 0.0 && balance <= 999.99)
    {
        printf("*** Your balance is getting low! ***\n");
    }
}

void printBankRecord(float balance_0, float balance, int numDeposits, int numWithdrawals, float depositRecords[5], float withdrawalRecords[5])
{
	
    printf("\n");
    
    printf(" *** Bank Record ***\n\n");
    printf("Starting Balance: $ %15.2f\n\n", balance_0);
    
    
int i=0;
for (i = 0; i < numDeposits; i++)
    {
        printf("Deposit #%d:         %.2f\n", i + 1, depositRecords[i]);
    }
    
    for (i = 0; i < numWithdrawals; i++)
    {
        printf("Withdrawal #%d:      %.2f\n", i + 1, withdrawalRecords[i]);
    }
    
    printf("\n");
    
    printf("Ending Balance: $   %.2f\n\n", balance);
    
}


What I have tried:

It keeps saying that i is only declared once. I'm not sure how to fix this.

thanks.
Posted
Updated 30-Jul-18 0:13am
v2
Comments
Patrice T 28-Jul-18 19:01pm    
Tell exact error message and position.
Richard MacCutchan 29-Jul-18 5:06am    
What is 'it'? Please show the complete error message and explain which line it refers to.

Not sure what this program is supposed to do but here are some suggestions.
Start here:
C++
/* Function Definitions*/
float promptInitialBalance(void)
{
    float balance_0 = 0.0;     /* starting balance */
    
    /* continuously prompt user for a starting account balance*/
    while (balance_0 < 0.0)
    {
        printf("\nEnter your current balance in dollars and cents: ");
        scanf("%lf", &balance_0);

balance_0 is a float so:
C++
scanf("%f", &balance_0);

also:
C++
while (balance_0 < 0.01)

There may be better ways but this is a start.
also:
Dont' call
C++
promptInitialBalance()
twice from main.

Next learn how to use a debugger before posting more questions.
Begin by stepping through the code in your mind just as I did.
 
Share this answer
 
v6
Comments
Patrice T 28-Jul-18 21:27pm    
'i is only declared once.'
Since it look like a compiler message, I fear debugger will not help much.
[no name] 28-Jul-18 21:37pm    
I've got no idea what you and the OP are referring to. i is only declared once and that is not the problem. When the OP makes the changes I suggested and gets out the debugger they will get somewhere.
Patrice T 28-Jul-18 22:00pm    
I only refer to what he said in 'What I have tried:'
But I haven't found anything related to this quote.
Your prompt-functions miss all while loop, because initial value made loop's condition 'false'.

float promptInitialBalance(void)
{
   float balance_0 = 0.0; //  should be -->   float balance_0 = -1.0f;  
    
    /* continuously prompt user for a starting account balance*/
    while (balance_0 < 0.0)
    {
    ...


...
int promptIntendedNumDeposits(void)
{
    int numDeposits = 0;  //  should be -->  int numDeposits = -1;
    
    /* continuously prompt user for the number of intended deposits */
    while (numDeposits < 0 || numDeposits > 5)
    {
    ...

...
int promptIntendedNumWithdrawals(void)
{
    int numWithdrawals = 0;   //  should be -->  int numWithdrawals = -1; 
    
    /*continuously prompt user for the number of intended withdrawals*/
    
    while (numWithdrawals < 0 || numWithdrawals > 5)
    {
    ...



And

scanf("%lf", &balance_0);

If you compile with -Wall, you will see warning like warning: format '%lf' expects argument of type 'double *', but argument 2 has type 'float *' [-Wformat=]
Correct is:
scanf("%f", &balance_0);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900