Search code examples
c++banking

Calculating year end balance and year end interest with compound interest


I am trying to create a function that calculates year end balance and year end interest based on the monthly deposits and monthly interests that accumulate in the year. For some reason, it is not outputting the correct numbers. Whenever I input 1 for investment, 50 for monthly deposit, %5 for annual interest, and 5 for years. It should output:

Year Year End Balance Year End Earned Interest

1      $617.55               $16.55

2      $1265.65               $48.10

3      $1946.90               $81.25

4      $2663.01               $116.11

5      $3415.76               $152.75

Instead, it is outputting:

Year Year End Balance Year End Earned Interest

1      $600.76               $0.16

2      $1205.65               $0.63

3      $1817.61               $1.39

4      $2440.90               $2.45

5      $3078.92               $3.82

Can someone help with function reportWithMonthlyPay()?

Here is the code I have currently:

// BankingApp.cpp : This file contains the 'main' function. Program execution begins and 
ends there.
//

#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;

class Bank {
public: 
    void dataInput();
    void displayInput();
    void reportWithoutMonthlyPay();
    void reportWithMonthlyPay();

private:
    double investment;
    double deposit;
    int years;
    double interest;
    double monthly_interest;
    double monthly_balance;
    double year_end_balance;
    double year_end_interest;
    vector<int> month_numbers;
    vector<int> year_numbers;
};

void Bank::dataInput() {
    cout << "Initial Investment Amount: " << endl;
    cin >> investment;
    cout << "Monthly Deposit: " << endl;
    cin >> deposit;
    cout << "Annual Interest: " << endl;
    cin >> interest;
    cout << "Number of years: " << endl;
    cin >> years;
    system("pause");       // Windows-specific command that tells 
platform to pause 
program
    cout << endl;
    if (cin.get()) {        // If any key is entered, displayInput 
 function is called
        displayInput();     
    }

}

void Bank::displayInput() {
    cout << "Initial Investment Amount: " << "$" << investment << 
 endl;
    cout << "Monthly Deposit: " << "$" << deposit << endl;
    cout << "Annual Interest: " << interest << "%" << endl;
    cout << "Number of years: " << years << endl;
    system("pause");
    cout << endl;
    reportWithoutMonthlyPay();
   }

  void Bank::reportWithoutMonthlyPay() {
    year_numbers.resize(years);
    interest = interest / 100;
    year_end_balance = investment + (investment * interest);
    year_end_interest = investment * interest;
     cout << "   Balance and Interest Without Additional Monthly 
 Deposit   " << endl;
    cout << "=============================================================" << 
  endl;
    cout << "Year    Year End Balance    Year End Earned Interest" 
  << endl;
    cout << "-------------------------------------------------------------" << endl;
    for (int i = 0; i < year_numbers.size(); i++) {
        year_numbers[i] = i + 1; 
        cout << year_numbers[i] << "        $" << fixed << 
  setprecision(2) << 
    year_end_balance << "               $" << year_end_interest << 
  endl;
        year_numbers[i]++;
        year_end_balance += year_end_interest;
        year_end_interest = year_end_balance * interest;
    }
    cout << endl;
    system("pause");
    reportWithMonthlyPay();
}

 void Bank::reportWithMonthlyPay() {
 year_numbers.resize(years);
 month_numbers.resize(12);
 year_end_interest = 0;
 year_end_balance = investment;
  cout << "   Balance and Interest With Additional Monthly Deposit   
  " << endl;
   cout << 
  "==========================================================" << 
 endl;
   cout << "Year     Year End Balance   Year End Earned Interest" 
  << endl;
   cout << "------------------------------------------------------- 
---" << endl;
   for (int i = 0; i < year_numbers.size(); i++) {
       year_numbers[i] = i + 1;
       for (int j = 0; j < month_numbers.size(); j++) {
        year_end_balance += deposit;
        monthly_interest = year_end_balance * (interest / 
(double)12);
        year_end_balance += monthly_interest;
        year_end_interest += monthly_interest;
        month_numbers[j]++;
    }
    cout << year_numbers[i] << "        $" << fixed << 
   setprecision(2) << year_end_balance << "               $" << 
   year_end_interest << endl;
    year_numbers[i]++;
   }

int main()
{
    Bank userInput;

    userInput.dataInput();
}

Solution

  • ok, well i run your code and its all over the place. inputting 1,50,5,5 as entered in the question is doesnt do any monthly payment processing because it only calls reportWithoutMonthlyPay

    If I change the code to call reportWithMonthlyPay the output is nothing like you show. So then I fixed one line and the output is close

                monthly_interest = year_end_balance * (interest /  (double)1200);
    

    in reportWithMonthy. Ie scale down interest by 100.

    output is

    Year     Year End Balance   Year End Earned Interest
    -------------------------------------------------------  -- -
    1        $617.55               $16.55
    2        $1265.65               $64.65
    3        $1946.90               $145.90
    4        $2663.01               $262.01
    5        $3415.76               $414.76
    

    still not like what you gave as expected output but closer. I leave it to you to find out why

    OK cracked that too. You need to reset year_end_interes to 0 for each year loop in reportWithMonthlyPay. When I do that

    ==========================================================
    Year     Year End Balance   Year End Earned Interest
    -------------------------------------------------------  -- -
    1        $617.55               $16.55
    2        $1265.65               $48.10
    3        $1946.90               $81.25
    4        $2663.01               $116.11
    5        $3415.76               $152.75
    

    so A for me on the assignment