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;
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.
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.