Write your question here.
My concern is the do-while loop.
My output is:
Enter the old and new consumer price indices Inflation rate is -0.0390204
Enter the old and new consumer price indices:Try again? y or Y
However, the expect output for my lab is
Enter the old and new consumer price indices: Inflation rate is -0.0390204
Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is -0.167049
Try again? (y or Y): Enter the old and new consumer price indices: Inflation rate is 0.0752572
Try again? (y or Y): Average rate is -0.0436042
Please help and be as specific as possible. Thank you
//This program calculates the inflation rate given two Consumer Price Index values and prints it to the monitor.
#include <iostream>
usingnamespace std;
/*
* InflationRate - calculates the inflation rate given the old and new consumer price index
* @param old_cpi: is the consumer price index that it was a year ago
* @param new_cpi: is the consumer price index that it is currently
* @returns the computed inflation rate or 0 if inputs are invalid.
*/
double InflationRate(float old_cpi, float new_cpi);
int main() //C++ programs start by executing the function main
{
// TODO #1: declare two float variables for the old consumer price index (cpi) and the new cpi
float old_cpi, new_cpi;
cout << "Enter the old and new consumer price indices ";
// TODO #2: Read in two float values for the cpi and store them in the variables
cin >> old_cpi >> new_cpi;
// TODO #3: call the function InflationRate with the two cpis
//call the function and add a variable and assign it.
double Rate = InflationRate(old_cpi, new_cpi);
// TODO #4: print the results
cout << "Inflation rate is " << Rate << endl;
//Part 2 add a loop
char userInput;
do
{
float old_cpi, new_cpi;
cout << "Enter the old and new consumer price indices: ";
cin >> old_cpi >> new_cpi << endl;
cout << "Try again? y or Y" << endl;
cin >> userInput;
}
while (userInput == 'y' || userInput == 'Y');
return 0;
}
// double InflationRate(float old_cpi, float new_cpi)
// precondition: both prices must be greater than 0.0
// postcondition: the inflation rate is returned or 0 for invalid inputs
double InflationRate (float old_cpi, float new_cpi)
{
// TODO: Implement InflationRate to calculate the percentage increase or decrease
if(old_cpi<0||new_cpi<0||old_cpi==0)
{
return 0;
}
return (new_cpi - old_cpi) / old_cpi * 100;
// Use (new_cpi - old_cpi) / old_cpi * 100
}