Loop keeps returning original input?

Hi there - very, very new to C++. Tried to look for a similar problem on the forum (and other websites), but when comparing my program to successful ones, there doesn't seem to be much of a difference.

I'm supposed to create a program that takes the cost of an item, number of years before it will be purchased, and the inflation rate to discover the cost of the item in the designated number of years. My problem is that my code always returns the original cost no matter what I do (so in ten years, a $5 product will cost the same despite any inflation).

The code I'm using currently is this:

#include <iostream>
using namespace std;

int main ( )

{


int years ;

int inflationRate ;

double costOfItem ;


cout << "In how many years will you be purchasing the item? ";
cin >> years ;
cout << "Enter the current cost of the item " ;
cin >> costOfItem;
cout << "Enter the inflation rate (percentage) " ;
cin >> inflationRate;

for (int count = 0; count <= years; ++count);

{

costOfItem = ((inflationRate/100)*costOfItem) + costOfItem ;





}

cout << "Your item will cost $"
<< costOfItem ;

return 0;

}


I guess I can see why it would return the same input value, but then I'm not sure how to compound the interest.

Sorry if I'm just doing something retarded, but a fresh set of eyes at the very least would be much appreciated.
inflationRate/100
Integer division. Divide by 100.0, not 100.

Why is inflationRate an integer in the first place?
Last edited on
Since you're dealing with prices and percentages, use float instead of int.

Also, in your for loop, you aren't actually compounding anything. You are setting costOfItem equal to the same expression over and over again.

Use "costOfItem +="
The += will add the interest to the current value of costOfItem and set the result as costOfItem's new value.
Okay, I added the "costOfItem +=" and it's returning a different value (more than it's ever done before, so thank you so much), but now it only does it for one year.

for (int count = 0; count <= years; ++count);

{

costOfItem += ((inflationRate/100.0)*costOfItem) ;


}

So it's not really looping?
Daleth wrote:
Since you're dealing with prices and percentages, use float instead of int.
Two problems. Number One, NEVER use float unless you are forced to. Always use at least double or long double. Number Two, with money, you should always use integral types to represent the number of pennies. Floating point types are not accurate.
Daleth wrote:
Also, in your for loop, you aren't actually compounding anything. You are setting costOfItem equal to the same expression over and over again.
False. It uses its previous value to calculate the next.
Daleth wrote:
Use "costOfItem +="
The += will add the interest to the current value of costOfItem and set the result as costOfItem's new value.
NO. This would be WRONG.
W300mphblues wrote:
Okay, I added the "costOfItem +=" and it's returning a different value (more than it's ever done before, so thank you so much), but now it only does it for one year.
Just use = and not +=, I think Daleth misread.
Topic archived. No new replies allowed.