Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#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
Comments
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.

1 solution

you must store the inflationrate in a variable with a bigger scope
C++
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;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900