# inflation program using functions

• 10-14-2010
rfoor
inflation program using functions
hello

im writing a program that will give the rate of inflation for the past year. it asks for the price of an item for this time and the price of the same item a year ago. i want to give the inflation rate as a percent with the type double. i dont understand what is going wrong with it, it will skip the second input if i use decimals, and the inflation comes out weird. please give me some hints about it.

Code:

``` #include<iostream> using namespace std; double inflationrate(int now, int yearago); int main() {         int now, yearago;         double inflation;         cout<<"Enter the price of something today: \$";         cin>>now;         cout<<"Enter the price of the same thing one year ago: \$";         cin>>yearago;         cout<<"You have an inflation of "<<inflationrate<<"%\n";                 system("pause");         return 0; } double inflationrate(int now, int yearago) {         return (((now-yearago)/yearago));                // inflation rate is what is inside the parentheses }```
• 10-14-2010
rags_to_riches
Have you never called a function before? You're not calling the inflationrate function properly. Also you're going to fall victim to integer division if you don't cast the divisor into a double.
• 10-14-2010
jimblumberg
Code:

```        int now, yearago;     double inflation;     cout<<"Enter the price of something today: \$";     cin>>now;     cout<<"Enter the price of the same thing one year ago: \$";     cin>>yearago;```
You are using int for your input. Ints do not have decimal points. If you want decimal points use floating point (float, double).

Jim
• 10-14-2010
Chomp
Yes, as they have mentioned, you are doing a few things incorrectly.

First, an integer is fine to store the cost of something. Many currencies are based on the equivalent of the U.S. penny rather than the U.S.D. However, in this case, I'm assuming that you're working with U.S. currency and as such you should probably use a float or a double.

Furthermore, given what you want to accomplish in this program, there's no reason not to use a float or a double. Integers cannot properly be divided to a decimal without first being cast as a float or a double, so you may as well use a float or a double to store the prices.

Additionally, when you call a function you must supply it arguments. Sometimes we assume that the computer can tell what we want it to do, but it can only do what we tell it to do. The computer doesn't know you want to perform the function on the now and yearago variables because you haven't supplied them as arguments to the function.

So, you need to change the function call to:

Code:

`inflationrate(now, yearago)`
Furthermore, you'll probably want to change the inflationrate function to take floats or doubles as arguments or cast the integers you're supplying into floats or doubles.