Compound interest in for loop trouble

PLESE DELETE
Last edited on
balance += annualDeposit * yearsToInvest;

Looks like your code puts the total amount in at the start of the first year, instead of 500 in the first year and then 500 in the second.
NOTHING
Last edited on
maybe my calculations are wrong?
come on guys I need help :/ I need to solve this by tomorrow.
??
I get this as my ouput without adding in any values:

Output:
Enter deposit amount: Another (y or n)? 
Total deposits: 0
Number of deposits: 1
Average deposit: 0

You want to invest in a new account that automatically deposits this average
amount once each year. The money earns interest, compounded annually, and
you want to know how much interest will be earned and the final balance.

Enter number of years to invest: 
Enter annual interest rate (e.g. 8.5): 

Number of years invested: 134541077
Interest Rate: 4.00027e-34
Amount Invested: 0
Interest Earned: 0
Final Balance: 0



Seems like there might be a problem with number of deposits?
Try changing everything to a double data type?
Last edited on
wow thats interesting. Had no idea. Wish I knew how to source it out. I spent so much time on this, I dont know where to look. Everything seem to make sense until the FOR loop.

finally got it :D
Last edited on
OP for reference.

Ive been working on this program for over 12 hours and I've been stuck on this compound interest part for over 4 hours. I made an account just to get some feedback on this. But im sure I will be on this forum a lot more.
Well here's the code.

The Problem is in PART 2 in the for loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
	   //Input Variables

	char userResponse;
	float depositAmount;
	float totalDeposits;

	int depositCount;
	float averageDeposit;

	// Accumulators

	depositCount = 0;
	totalDeposits = 0;
	

do 
{
	cout << "Enter deposit amount: "; 
	cin >> depositAmount;
	
	totalDeposits =+ depositAmount + totalDeposits;
	depositCount++;
		
	cout << "Another (y or n)? ";
	cin >> userResponse;

	
}while (userResponse == 'Y' || userResponse == 'y' );

//calculations

averageDeposit = totalDeposits / depositCount;

//output
cout << endl;
cout << "Total deposits: " << totalDeposits << endl;
cout << "Number of deposits: " << depositCount << endl;
cout << "Average deposit: " << averageDeposit << endl << endl;

//PART 2
// Message about investing in new interest bearing acount.

cout << "You want to invest in a new account that automatically deposits this average" << endl;
cout << "amount once each year. The money earns interest, compounded annually, and"    << endl;
cout << "you want to know how much interest will be earned and the final balance."     << endl;

// Declare Variables

float interestRate;
int yearsToInvest;
float annualDeposit;
float balance;
float amountInvested;
float interestEarned;

// Accumulator / Pre-calculations

balance = 0;

annualDeposit = averageDeposit;



cout << endl;
cout << "Enter number of years to invest: ";
cin >> yearsToInvest; 
cout << endl;


cout << "Enter annual interest rate (e.g. 8.5): ";
cin >> interestRate;
cout << endl;




for (int yearCounter = 1; yearCounter <= yearsToInvest; yearCounter++);
{
		
		balance += annualDeposit * yearsToInvest;
		
		
		balance = balance * pow(1 + interestRate/100, yearsToInvest) ;
		
	
		 
}

//Calculations

amountInvested = annualDeposit * yearsToInvest;
interestEarned = balance - amountInvested;

//Output

cout << endl;
cout << "Number of years invested: " << yearsToInvest << endl;
cout << "Interest Rate: "  << interestRate << endl;
cout << "Amount Invested: " << amountInvested << endl;
cout << "Interest Earned: " << interestEarned << endl;
cout << "Final Balance: " << balance << endl;



	return 0;
}




The program runs fine when I'm using only '1' for number of years invested and deposits.. However, when I use 2 or more years for number of years invested, the program doesn't do the math correctly.

Enter deposit amount: 500
Another (y or n)? n

Total deposits: 500
Number of deposits: 1
Average deposit: 500

You want to invest in a new account that automatically deposits this average
amount once each year. The money earns interest, compounded annually, and
you want to know how much interest will be earned and the final balance.

Enter number of years to invest: 2

Enter annual interest rate (e.g. 8.5): 8.5


Number of years invested: 2
Interest Rate: 8.5
Amount Invested: 1000
Interest Earned: 177.225
Final Balance: 1177.23
Press any key to continue . . .

FINAL BALANCE SHOULD BE 1131.11!
Topic archived. No new replies allowed.