INFLATION.CPP Write a program that gauges the rate of inflation over the past ye
ID: 3592821 • Letter: I
Question
INFLATION.CPP Write a program that gauges the rate of inflation over the past year. The program asks for the price of an item (such as a hot dog or a 1-carat diamond) both from one year ago and today. It estimates the inflation rate as the difference in price divided by the year-ago price. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the rate of inflation. The inflation rate should be a value of type double giving the rate as a percent, for example 5.32 for 5.32 percent. The inflation rate must be displayed to exactly two digits after the decimal point. REQUIREMENT: Your program must use a function to compute the rate of inflation. A program which does not use a function will be awarded a score of zero, even if all tests pass. You are only permitted to use the iostream library. These requirements will be checked by the instructor and the TAs.Explanation / Answer
#include <iostream>
using namespace std;
double inflation(int newP,int oldP)
{
return ((newP-oldP)/(double)oldP)*100;
}
int main()
{
int oldP;
int newP;
while(1)
{
cout<<"Enter the old price (or zero to quit):"<<endl;
cin>>oldP;
if(oldP==0)
break;
else
{
cout<<"Enter the new price:"<<endl;
cin>>newP;
cout<<"The inflation rate is " <<inflation(newP,oldP)<<"%"<<endl;
}
}
}
#include <iostream>
using namespace std;
double inflation(int newP,int oldP)
{
return ((newP-oldP)/(double)oldP)*100;
}
int main()
{
int oldP;
int newP;
while(1)
{
cout<<"Enter the old price (or zero to quit):"<<endl;
cin>>oldP;
if(oldP==0)
break;
else
{
cout<<fixed;
cout.precision(2);
cout<<"Enter the new price:"<<endl;
cin>>newP;
cout<<"The inflation rate is " <<inflation(newP,oldP)<<"%"<<endl;
}
}
}
If you don't want to include using name space std then you will have to add std:: in front of each cout and cin otherwise it works fine with this also.
Please give it a thumbs up.