Enhance your program from the previous exercise by having it also print out the estimated price of the item in one and in two years from the time of the calculation. The increase in cost over one year is estimated as the inflation rate times the price at the start of the year. Define a second function to determine the estimated cost of an item in a specified number of years, given the current price of the item and the inflation rate as arguments.
Program Code:
#include<iostream>
#include<cmath>
using namespace std;
//declare the method to calculate inflation
double calcInflation(double previousCost,double currentCost);
//declare the method to calculate cost after inflation
double estimateInflationCost(double currentCost, double inflationRate);
int main()
{
//declare the variables
double previousCost, currentCost, inflationRate, increasedCost;
char option;
//loop to iterate till the user choose to continue
do
{
//prompt and read cost of the item in the previous year
cout<<"Enter previous year cost of the item: ";
cin>>previousCost;
//prompt and read cost of the item in the current year
cout<<"Enter present year cost of the item: ";
cin>>currentCost;
//call method to calculate cost of inflation
inflationRate = calcInflation(previousCost,currentCost);
//diaply cost of inflation
cout<<"Rate of inflation: "<<inflationRate*100<<"%"<<endl;
//call method to calculate and display cost of item after one year due to inflation
increasedCost = estimateInflationCost(currentCost, inflationRate);
cout<<"Cost of the item after one year due to inflation: "<<increasedCost<<endl;
//call method to calculate and display cost of item after two year due to inflation
increasedCost = estimateInflationCost(increasedCost, inflationRate);
cout<<"Cost of the item after two years due to inflation: "<<increasedCost<<endl;
//prompt and read the user choice to continue or not
cout<<"To continue then enter 'Y':";
cin>>option;
}while((option=='Y')||(option=='y'));
return 0;
}
//define the method to calculate inflation
double calcInflation(double previousCost,double currentCost)
{
//calculate and return the inflation rate
return ((currentCost - previousCost)/currentCost);
}
//define the method to calculate cost after inflation
double estimateInflationCost(double currentCost, double inflationRate)
{
//calculate and return the increase in cost due to inflation after one year
return currentCost+(currentCost*inflationRate);
}
Sample output: