The function call does not display cost

advertisements

Hello I've written a program to calculate the ending balance of a bank account. I've created an overloaded function to add the deposits to the starting balance, one for an interest amount and one without any interest. Currently, the function call does not display any value.

Any input as to why this happens would be greatly appreciated! And yes, I have stepped through the code line by line and explained it to my rubber duck.

#include <iostream>

using namespace std;

const int SIZE = 25;
const int sentinel = -999;

int main(){
    void balanceAccount(double, int, double[]);
    void balanceAccount(double, int, double[], double);
    double balance = 0.0, interest = 0.0, num = 0.0, total = 0.0;
    double deposit[SIZE] = {};
    int count = 0;

    cout << "Enter starting balance: $";
    cin >> balance;

    while (num != sentinel && count < SIZE)
    {
        cout << "Enter up to 25 deposits, or -999 to quit: " << endl;
        cin >> num;
        if (num != sentinel){
            deposit[count] = num;
            count++;
        }
        cout << "There are " << count << " deposits this week." << endl;
    }
    cout << "Test deposits: ";
    for (int i = 0; i < count; i++){
        cout << deposit[i] << " ";
    }

    cout << "Enter interest amount: ";
    cin >> interest;
    if (interest == 0.0){
        balanceAccount(balance, count, deposit);
    }
    else if (interest > 0.0){
        balanceAccount(balance, count, deposit, interest);
    }

    system("pause");
    return 0;
}

void balanceAccount(double bal, int ct, double dep[]){
    double sum = 0.0;
    for (int i = 0; i < ct; i++){
        sum += dep[i];
    }
    sum = sum + bal;

    cout << "Ending balance: $" << sum << endl;
}
void balanceAccount(double bal, int ct, double dep[], double rate){
    double interestAmount = 0.0, sum = 0.0;
    for (int i = 0; i < ct; i++){
        sum += dep[i];
    }
    sum = sum + bal;
    interestAmount = sum * rate;
    sum = sum + interestAmount;

    cout << "Ending balance: $" << sum << endl;
}


Your condition in first if does not compare interest against value 0, but rather assigns 0 to the interest variable. Result of such condition is 0 which is interpreted as false.
Next if (else-if) checks if the interest is positive, but since you changed it in previous condition to 0, it is false again.

Change assignment into comparison using == instead of =.