-1

Currently doing a project for a course in C++, where I need to make a table of a deposited amount, interest earned on the amount, and total interest earned increased by each year.

I'm not getting the output I'm looking for. It just gives me the same output for each year. I am sure my problem lies in my for loop statements, but I need a fresh set of unbiased eyes as I am new to this. first is .cpp

#include <iostream>
#include <iomanip>
#include "Investment.h"

using namespace std;

    int main() {
        //Declare variables for input
        double initialDeposit, monthlyDeposit, interestRate, years, months;

        //Declare variables for data
        double totalInterestEarned;

        Investment userInput;
        //Display menu to the user
        cout << "**********************************" << endl;
        cout << "*********** Data Input ***********" << endl;
        cout << "Initial Investment Amount: " << endl;
        cout << "Monthly Deposit: " << endl;
        cout << "Annual Interest: " << endl;
        cout << "Number of years: " << "\n" << endl;


        //Get user input
        cout << "**********************************" << endl;
        cout << "*********** Data Input ***********" << endl;
        cout << "Initial Investment Amount: $" << endl;
        cin >> initialDeposit;
        userInput.setInitialDeposit(initialDeposit);
        cout << "Monthly Deposit: $" << endl;
        cin >> monthlyDeposit;
        userInput.setMonthlyDeposit(monthlyDeposit);
        cout << "Annual Interest: %" << endl;
        cin >> interestRate;
        userInput.setInterestRate(interestRate);
        cout << "Number of years: " << endl;
        cin >> years;
        userInput.setNumYears(years);
        months = years * 12;

        totalInterestEarned = userInput.interestEarned(initialDeposit, interestRate);




        //Display year end data if no monthly deposits

        cout << endl << "Balance and Interest Without Additional Monthly Deposits" << endl;
        cout << "================================================================" << endl;
        cout << "Year          Year End Balance          Year End Earned Interest" << endl;
        cout << "----------------------------------------------------------------" << endl;
        userInput.grandTotal(totalInterestEarned, initialDeposit, years);


        //Display year end data with monthly deposits
        cout << endl << "Balance and Interest With Additional Monthly Deposits" << endl;
        cout << "================================================================" << endl;
        cout << "Year          Year End Balance          Year End Earned Interest" << endl;
        cout << "----------------------------------------------------------------" << endl;
        userInput.grandTotalMonthly(totalInterestEarned, initialDeposit, interestRate, years);


        return 0;
    }

second is Investment.h

#include <iostream>
#include <iomanip>
#ifndef Investment
using namespace std;
class Investment {
public:
    //Data variables needed here for this class
    double initialDeposit = 0.0;
    double interestRate = 0.0;
    double monthlyDeposit = 0.0;
    double years = 0.0;

public:
    //Constructor here
    Investment() {}

    void setInitialDeposit(double fromUserInput)
    {
        initialDeposit = fromUserInput;
    }
    double getInitialDeposit()
    {
        return initialDeposit;
    }
    void setInterestRate(double fromUserInput)
    {
        interestRate = fromUserInput;
    }
    double getInterestRate()
    {
        return interestRate;
    }
    void setMonthlyDeposit(double fromUserInput)
    {
        monthlyDeposit = fromUserInput;
    }
    double getMonthlyDeposit()
    {
        return monthlyDeposit;
    }
    void setNumYears(double fromUserInput)
    {
        years = fromUserInput;
    }
    double getNumYears()
    {
        return years;
    }
    double interestEarned(double initialDeposit, double interestRate) {
        double totInterest = (initialDeposit + (interestRate / 100.0));
        return totInterest;
    }
    void grandTotal(double interestEarned, double initialDeposit, double years) {
        //Calculate yearly interest and year end total
        for (int i = 0; i < years; i++) {
            //Calculate yearly interest amount
            interestEarned = ((initialDeposit) * (interestRate / 100));

            //Calculate year end total
            double totalAmount = initialDeposit + interestEarned;

            //Show decimal as dollar amount correctly with set precision to 2 decimal places
            cout << (i + 1) << "\t\t$" << fixed << setprecision(2) << totalAmount << "\t\t\t$" << interestEarned << endl;
        }
    }
    void grandTotalMonthly(double interestEarned, double intiialDeposit, double interestRate, double years) {
        for (int i = 0; i < years; i++) {
            //Initialize yearly interest to 0
            double yearlyTotalInterest = 0.0;
            double totalAmount = initialDeposit;

            for (int j = 0; j < 12; j++) {
                //Calculate monthly interest amount
                interestEarned = (((initialDeposit + monthlyDeposit) * (interestRate / 100)) / 12);

                //Calculate month end interest
                yearlyTotalInterest = yearlyTotalInterest + interestEarned;

                //Calculate month end total
                totalAmount = totalAmount + monthlyDeposit + interestEarned;
            }

            cout << (i + 1) << "\t\t$" << fixed << setprecision(2) << totalAmount << "\t\t\t$" << yearlyTotalInterest << endl;
        }
    }
};

#endif

below is current, wrong output of program for clarity.

**********************************
*********** Data Input ***********
Initial Investment Amount: $
1000
Monthly Deposit: $
5
Annual Interest: %
5
Number of years:
5

Balance and Interest Without Additional Monthly Deposits
================================================================
Year          Year End Balance          Year End Earned Interest
----------------------------------------------------------------
1               $1050.00                        $50.00
2               $1050.00                        $50.00
3               $1050.00                        $50.00
4               $1050.00                        $50.00
5               $1050.00                        $50.00

Balance and Interest With Additional Monthly Deposits
================================================================
Year          Year End Balance          Year End Earned Interest
----------------------------------------------------------------
1               $1110.25                        $50.25
2               $1110.25                        $50.25
3               $1110.25                        $50.25
4               $1110.25                        $50.25
5               $1110.25                        $50.25
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
Matt Trembley
  • 31
  • 1
  • 9
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel May 30 '21 at 02:02
  • 1
    SO isn't made for code reviewing, but for specific questions, with a behaviour given by a minimal reproductible example – Vélimir May 30 '21 at 02:02
  • 1
    Also I don't see why your value would be changing, as in grandTotal you aren't taking the year (i) into account, and you don't pile the iteration result anywhere else either – Vélimir May 30 '21 at 02:04
  • @Sho Understandable. Just thought I'd give it a shot - still on the learning curve of SO. – Matt Trembley May 30 '21 at 02:04
  • @MattTrembley Yep, sorry if it sounded rude. It's pretty common to mistake the two – Vélimir May 30 '21 at 02:05
  • @Sho at first I took it as rude, then thought to myself "they're just trying to help!" But actually I didn't see that I wasn't taking year(i) into the equation - which is the solution to my question! – Matt Trembley May 30 '21 at 02:08
  • 1
    You only need ONE call to `std::cout` for any continual block of text, not one `std::cout` for each line. That's what `'\n'` is for... – David C. Rankin May 30 '21 at 02:11

2 Answers2

1

I don't know much about interest rates, but in grandTotal you probably forgot a power in

            //Calculate yearly interest amount
            interestEarned = ((initialDeposit) * (interestRate / 100));

It should be

            //Calculate yearly interest amount
            interestEarned = ((initialDeposit) * std::pow(interestRate / 100, i));

as the interest grows for each year

Vélimir
  • 409
  • 3
  • 12
1

As @Sho pointed out to me, I wasn't taking the year (i) into account of my equations, which led to me solving my output issue.

Matt Trembley
  • 31
  • 1
  • 9