giving wrong answers

So I am new to c++ and we just learned functions. My program always gives back answers like -6.98765e24 and I can't seem to track down why. It might be something in my function or something else, like I said I really don't know that much about writing programs. Thank you!


#include <iostream>
#include <cmath>
using namespace std;

main()
{
double pricetoday, pricepast, rate;
char answer;

do
{
cout << "Enter the price of an item today\n";
cin >> pricetoday;
cout << "Enter the price of that item one year ago\n";
cin >> pricepast;

cout << "The inflation rate is: " << rate << " percent.\n";

cout << "Do you want to calcluate the inflation on another item?\n";
cout << "Press y or n and then press enter\n";
cin >> answer;
} while ((answer == 'y') || (answer == 'Y'));
cout << "End program." <<endl;
}

double rate(double pricetoday, double pricepast)
{

double rate;
rate = (pricetoday - pricepast)/pricepast;
return rate;
}

1) You are creating a local variable called "rate" that is never assigned to anything, hence why you are getting garbage results.

2) You are never calling your rate function.

3) You are also not prototyping your rate function, so you can't even call it from main if you wanted to.

4) main shall return an int.
Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

Nowhere in your code do you ever actually call your rate() function. Also, nowhere in your code do you ever assign a value to the local variable also called rate in your main function. So you're just printing out whatever value happens to be in that memory location.
Topic archived. No new replies allowed.