Click here to Skip to main content
14,271,663 members
Rate this:
Please Sign up or sign in to vote.
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
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

Rate this:
Please Sign up or sign in to vote.

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;
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100