trouble with a for loop

I'm trying to build a program that will take a dollar amount, a percentage, and a number of years. In a for loop, the program is supposed to multiply the dollar amount by the percentage and add that number to the original dollar amount. However every time the for loop executes, instead of using the new dollar amount, the program uses the original dollar amount so that if more than one year was entered, the output will be incorrect. I believe my problem lies within my for loop. Here's the code:

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
#include <iostream>

using namespace std;

int main()
{
	double iDollar, percentage, cpercentage, total = 0, thisyear = 0;
	int years;

	cout << "Enter a numeric dollar value: ";
	cin >> iDollar;

	cout << "Enter estimated return percentage: ";
	cin >> percentage;

	cpercentage = percentage * 0.01;

	cout << "Enter number of years Money will be invested: ";
	cin >> years;

	for(int x = 0; x < years; x++)
	{
		thisyear = iDollar * cpercentage;
		total = iDollar + thisyear;
		cout << total << endl;
	}

	cout << endl;
	cout << "After " << years << " years, at " << percentage << "%, your stock will have earned $"
		<< (total - iDollar) << ", \nbringing your total amount to $" << total << endl;

	cin.get();
	cin.get();
	return 0;
}


Thanks ahead for you help :)
Your logic is wrong. Throw away iDollar, enter the first value into total.


1
2
3
4
5
6
for(int x = 0; x < years; x++)
	{
		thisyear = total * cpercentage;
		total = total + thisyear;
		cout << total << endl;
	}
cool thanks!
Topic archived. No new replies allowed.