inflation program using functions

This is a discussion on inflation program using functions within the C++ Programming forums, part of the General Programming Boards category; hello im writing a program that will give the rate of inflation for the past year. it asks for the ...

  1. #1
    new to c++
    Join Date
    Feb 2009
    Posts
    53

    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 
    }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    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.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,701
    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

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of functions
    By frktons in forum C Programming
    Replies: 29
    Last Post: 06-30-2010, 09:51 AM
  2. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  3. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 01:42 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. Help starting a C program with functions
    By jlmac2001 in forum C Programming
    Replies: 6
    Last Post: 10-12-2002, 02:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21