C++ : Write a program that outputs inflation rates for two successive years and
ID: 675187 • Letter: C
Question
C++ : Write a program that outputs inflation rates for two successive years and whether the inflation is increasing or decreasing. Ask the user to input the current price of an item and its price one year and two years ago. To calculate the inflation rate for a year, subtract the price of the item for that year from the price of the item one year ago and then divide the result by the price a year ago. Your program must contain at least the following functions: a function to get the input, a function to calculate the results, and a function to output the results. Use appropriate parameters to pass the information in and out of the function. Do not use any global variables. C++ : Write a program that outputs inflation rates for two successive years and whether the inflation is increasing or decreasing. Ask the user to input the current price of an item and its price one year and two years ago. To calculate the inflation rate for a year, subtract the price of the item for that year from the price of the item one year ago and then divide the result by the price a year ago. Your program must contain at least the following functions: a function to get the input, a function to calculate the results, and a function to output the results. Use appropriate parameters to pass the information in and out of the function. Do not use any global variables.Explanation / Answer
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
double getDoubleInput(string message);
double inflationRate(double oldPrice, double newPrice);
void Percent(double value);
int main()
{
double price1; // The current price of the item
double price2; // The price of the item one year ago
double price3; // The price of the item two years ago
double rate1; // The inflation rate for the first two years
double rate2; // The inflation rate for the second two years
price1 = getDoubleInput("Enter the current price:");
price2 = getDoubleInput("Enter the price from one year ago:");
price3 = getDoubleInput("Enter the price from two years ago:");
cout << endl;
rate1 = inflationRate(price2, price1);
cout << "Current year inflation: ";
Percent(rate1);
rate2 = inflationRate(price3, price2);
cout << "Inflation one year ago: ";
Percent(rate2);
cout << endl;
if (rate1 < rate2)
{
cout << "The inflation rate is increasing" << endl;
}
else if (rate1 > rate2)
{
cout << "The inflation rate is decreasing" << endl;
}
else
{
cout << "There is no change in the inflation rate" << endl;
}
system("pause");
return 0;
}
double getDoubleInput(string message)
{
double num;
cout << message << " ";
cin >> num;
while (!cin)
{
cin.clear();
cin.ignore(5000, ' ');
cout << "Please enter a double: ";
cin >> num;
}
return num;
}
double inflationRate(double oldPrice, double newPrice)
{
double rate;
rate = (newPrice - oldPrice) / oldPrice;
return rate;
}
void Percent(double value)
{
cout << fixed << setprecision(2);
cout << (value * 100) << "%" << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.