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