Fernanda Fernanda - 2 months ago 17
C++ Question

inflation rate calculator c++

I'm trying to build an inflation rate calculator but it wont work.
I'm currently out of the country and using a school pc so all the errors will appear in Korean, sorry i can t be more specific as i have no idea what they say.

include <iostream>
using namespace std;


double inflationRate (double startingPrice, double currentPrice)


void main()
{
double startingPrice, currentPrice, inflationRate;
char again;

do
{
cout << "enter the price of the item when you bought it: ";
cin >> startingPrice;

cout << "enter the price of the item today: ";
cin >> currentPrice;

cout << "the inflation Rate is: " << inflationRate << "%";

cout << "would you like to try another item (y/n)?";
cin >> again;

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

return 0;

}

double inflationRate(double startingPrice, double currentPrice)
{
return ((currentPrice - startingPrice) / startingPrice);
}

Answer

You're declaring a useless variable inflationRate instead of calling the function inflationRate. I think this is what you want (I'm assuming the missing # on the first line was a copy-paste error):

#include <iostream>
using namespace std;


double inflationRate (double startingPrice, double currentPrice);  // <-- Missing semicolon!


int main() // <-- Not void; use int.
{
double startingPrice, currentPrice; // <-- Removed inflationRate variable.
char again;

do 
{
    cout << "enter the price of the item when you bought it: ";
    cin >> startingPrice;

    cout << "enter the price of the item today: ";
    cin >> currentPrice;

    cout << "the inflation Rate is: " << inflationRate(startingPrice, currentPrice) << "%"; // <-- Calling inflationRate

    cout << "would you like to try another item (y/n)?";
    cin >> again;

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

return 0;

}

double inflationRate(double startingPrice, double currentPrice)
{
    return ((currentPrice - startingPrice) / startingPrice);
}