How can I ...
1. Put the logic in TODO #2-4 into a loop (do-while preferably if possible) that asks the user to enter 'y' (or 'Y') if there's more data to be entered.
2. Keep a running total of the valid inflation rates and the number of computed rates to calculate the average rate.
//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 indicies:";
// 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
{
cout << "Try again? [y or Y]"
}
while (userInput = 'y','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
}
//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 indicies:";
// 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
do
{
cout << "Try again? [y or Y]";
}
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
}
I don't understand why you removed it. You declared it in the first post. Re-add the declaration of your variable.
You also aren't looping over any actual code, that will just be a infinite loop as you have it.
You need to loop over the actual logic in your program.
1 2 3 4 5 6 7 8 9 10 11 12
int main()
{
char userInput;
do
{
// [ Actual logic in program here ]
// [ Ask user Try again Y/N]
} while (userInput == 'y' || userInput == 'Y');
}
You also will need std::cin >> userInput; somewhere within the do-while loop.
I tried this but i don't think its right and did not seem to compile. I'm not sure how much logic i want to take and put into the do-while loop. Do i just want to copy TODO #2-4 and put it into the do-while loop? but i tried that.
Ok... it runs but it is not giving the output correctly.
Example of test case (what it should be):
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
My output:
Enter the old and new consumer price indicies:Inflation rate is: -0.0390204
Enter the old and new consumer price indices:Inflation rate is 0
Try again? y or Y