Thread: Can't output a decimal number from a function result

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128

    Can't output a decimal number from a function result

    Hi,

    I have this simple code here:

    Code:
    #include <iostream>
    
    int multiply (double x, double y)
    {
        double result = x*y;
        return (result);
    }
    
    int main()
    {
        double a = 2.2;
        double b = 2.7;
        double c = multiply(a,b);
        std::cout << c;
    }
    And all I get as an output is 5. However, if I use this:

    Code:
    #include <iostream>
    
    int main()
    {
        double a = 2.2;
        double b = 2.7;
        double c = a * b;
        std::cout << c;
    }
    I get the answer 5.94 (which is what I'm looking for).

    I can't work out why the first example is not outputting a decimal number. I have set the variables as a double so I just can't see why this is not working for me.

    Any help is more than appreciated. I would prefer a hint rather than a straight answer just to set me in the right direction. Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Erm. It was only on reading my very own thread 2 seconds later that I noticed the beginning of the function...

    Code:
    int multiply
    Doh!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-05-2011, 11:32 PM
  2. Different Output Result
    By edmund085 in forum C Programming
    Replies: 7
    Last Post: 07-29-2011, 08:21 PM
  3. Typing decimal number and cover it binary number!
    By Kazumi in forum C Programming
    Replies: 32
    Last Post: 04-16-2011, 07:21 PM
  4. return decimal number back caller function
    By andy09 in forum C Programming
    Replies: 7
    Last Post: 10-10-2009, 08:10 AM