Hey, I'm new to this forum and to C++, having only just started this week by reading the 3rd edition of Absolute C++. I'm an art student looking to become proficient enough to use one of the pre-built XNA engines to get something running on a Zune. I look forward to becoming better and appreciate any help.
#include <cstdlib>
#include <iostream>
usingnamespace std;
int main()
{
int numberEntered;
int numberOfYears=numberEntered;
double pencilCost;
double inflationRate;
double estimatedCost;
cout << "This program will estimate the cost of 200 pencils x years from now.\n"
<< "To start, enter the current cost of a pencil: $";
cin >> pencilCost;
cout << endl;
cout << "Next, the number of years between now and the estimated year is: ";
cin >> numberEntered;
cout << endl;
cout << "Finally, the inflation rate which we are accounting for: ";
cin >> inflationRate;
cout << endl;
inflationRate=(inflationRate/100);
while (numberOfYears > 0)
{
numberOfYears--;
estimatedCost=(pencilCost * inflationRate) + pencilCost;
}
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "In " << numberEntered << " years, 200 pencils will cost $"
<< (estimatedCost * 200) << ".";
cin.ignore();
cin.get();
return 0;
}
I've been testing it with these inputs: pencilCost- 1.01, numberEntered/numberOfYears- 10, and inflationRate- 5.6. The thing is, when I punch out the equation on my calculator, I get $2,133.12 as the cost of the 200 pencils. The answer the program reads out, however, is $1,333.20. No idea what's going on, at first I thought it might've been the decimals, but I don't truncate anything until the very end. Also, it takes longer than I thought it would to evaluate, if that means anything.
Oh, you're right, that does make more sense. I've just moved it down to just before the loop, and the answer being read out now is, alarmingly, $213.31.
I think you're completely messing up the calculation, both on the calculator and on the program.
IINM, inflation is calculated by applying the ratio each year to the cost of the previous year. The cost for any year is the cost of the previous year times the rate, which means that for the original cost c after y years of r ratio, the cost is c*r^y. If we replace with your values, 1.01*1.056^10=1.741, which times 200 is 348.20.