14,271,663 members
Rate this:
See more:
```#include <iostream>
using namespace std;

double inflationRate (float old_cpi, float new_cpi);

int main()
{
float old_cpi, new_cpi;
char again;

do
{
cout << "Enter the old and new consumer price indices: ";
cin  >> old_cpi >> new_cpi;

cout << "Inflation rate is " << inflationRate(new_cpi, old_cpi) << endl;

cout << "Try again? (y or Y): ";
cin  >> again;

} while((again == 'Y') || (again == 'y'));

return 0;
}

double inflationRate(float old_cpi, float new_cpi)
{
return ((old_cpi - new_cpi) / new_cpi * 100);
}```

What I have tried:

How do I add this part: Keep a running total of the valid inflation rates and the number of computed rates to calculate average
Posted
Updated 5-Sep-18 22:06pm
v2
Mohibur Rashid 5-Sep-18 21:09pm

What happened? Have you tried debugging? and your functions first parameter is old but you are calling the function in wrong order.

Rate this:

## Solution 1

you must store the inflationrate in a variable with a bigger scope
```int main()
{
float old_cpi, new_cpi;
char again;

float totalInflation = 0;//scope of main

do
{
cout << "Enter the old and new consumer price indices: ";
cin  >> old_cpi >> new_cpi;

float inflationRate = inflationRate(new_cpi, old_cpi);//local variable
cout << "Inflation rate is " << inflationRate << endl;
totalInflation += inflationRate;//sum up total
cout << "Try again? (y or Y): ";
cin  >> again;

} while((again == 'Y') || (again == 'y'));

cout << "Total inflation rate is " << totalInflation << endl;```