### Help with loop not adding values to previous values

Hi,

I'm trying to write a program that will gauge the expected cost of an item in a specified number of years. I've looked at several examples and tried the recommendations but it is giving me the same value for each year instead of adding the previous year's price to the next year's price. This is my loop. Could someone offer me a hint at what am I overlooking please? Thanks.

cout << "Enter the cost of the item. \n";
double cost;
cin >> cost;
cout << "How many years from now will the item be purchased?\n";
double years;
cin >> years;
cout << "Enter rate of inflation.\n";
double infl;
cin >> infl;
double rateInfl = infl/100;
double amt = (rateInfl * cost) + cost;
int i;

for (i = 1; i <= years; i++)

cout << "The price of the item in " << i << "years will be: "<< amt << endl;
New here but I thought I might help. Do you have use a for loop?

Here is what I came up with without using a for loop.

 ``12345678910111213141516171819202122232425`` ``````#include #include #include using namespace std; int main () { double cost, infl, amt, years; cout << "Enter the cost of the item: "; cin >> cost; cout << "How many years from now will the item be purchased: "; cin >> years; cout << "Enter rate of inflation: "; cin >> infl; infl = (infl / 100); amt = cost * pow((1 + infl), years); cout << "The price of the item in " << years << " years will be: " << "\$" << fixed << setprecision(2) << amt; return 0; }``````
Last edited on
Figured it out with the loop for you. If you have any questions I'll do my best to answer.

 ``1234567891011121314151617181920212223242526272829`` ``````#include #include #include using namespace std; int main () { double cost, infl, years; cout << "Enter the cost of the item: "; cin >> cost; cout << "How many years from now will the item be purchased: "; cin >> years; cout << "Enter rate of inflation: "; cin >> infl; infl = (infl / 100); int i; for ( i=0; i < years; ++i) { cost += (cost * infl); } cout << "The price of the item in " << years << " years will be: " << "\$" << fixed << setprecision(2) << cost; return 0; }``````
Thanks!! It looks like I just had a couple lines in the incorrect order I guess and didn't have my loop completely written out. :( Yes, it needed to be written with a loop. I've been trying to teach myself the basics over the summer so I'd be ready for the class this fall. I was trying to go over a few problems in the first couple of chapters before we really started into it to see what it was going to be like. This was one of the simpler ones in chapter 2 but it seems like many of the problems in the textbook are way harder than what I worked with over the summer. :(

I have a feeling I may be on here quite frequently. Thanks again!!
Topic archived. No new replies allowed.