Supposed to return float but doesn't.

This is a discussion on Supposed to return float but doesn't. within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; float dividethesenumbers(int x, int y){ return x/y; } int main() { int x; int ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    15

    Supposed to return float but doesn't.

    Code:
    #include <iostream>
    
    using namespace std;
    
    float dividethesenumbers(int x, int y){
        return x/y;
    }
    
    int main()
    {
        int x;
        int y;
        cout << "2 numbers to be divided. \n First number: ";
        cin >> x;
        cout << "Second number: ";
        cin >> y;
        cout << dividethesenumbers(x,y) << endl;
        system("pause");
        return 0;
    }
    I'm still very wet behind the ears with C++, please bear with.

    I was going over function prototypes, to further remember the syntax, and came accross something I can't quite figure out.

    Using the above code, I compiled and used 22 for x and 7 for y. (Pi) Instead of getting 3.14......, I got 3.

    dividethesenumbers is supposed to return a float, right? I know I used integers for x and y as input but I thought the function would return the type I told it to.

    I know the fix is to replace the int x, int y with float x, float y (as well as in main()) but I don't understand why my function won't return the type I ask.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    The problem is your doing integer math, and therfor returning a integer automaticly converted to a float (after the math)

    inside your function, try something like
    return float(x)/float(y);
    instead. Now that isn't really the recomended way to do it, but it works, the actual recomended way would probably be

    return static_cast<float>(x)/static_cast<float>(y);

    try them both, they should give the same results, but the later is recomended (don't remeber why off the top of my head though)
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    15
    Made sense and answered my query, thanks.

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by Xipher
    The problem is your doing integer math, and therfor returning a integer automaticly converted to a float (after the math)

    inside your function, try something like
    return float(x)/float(y);
    instead. Now that isn't really the recomended way to do it, but it works, the actual recomended way would probably be

    return static_cast<float>(x)/static_cast<float>(y);

    try them both, they should give the same results, but the later is recomended (don't remeber why off the top of my head though)
    static_cast and c-style cast are basically the same. The benifits of the static_cast is that they stick out and clearly indicate what is being done. Where the c-style looks like a function call(Especially how you did it) which is why c-style cast are recommended to be formatted like:

    return (float)x/(float)y;

    Since I know float is an instrinsic type, i would know yours was a cast, but had that been a user-defined type, I wouldn't know by looking at it if it was a cast or a function call.

    Another benefit I often see listed is that you can do a search of your code for static_cast where you can't on the c-style cast.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Darryl
    but had that been a user-defined type, I wouldn't know by looking at it if it was a cast or a function call.
    Well the location of the parenthesis might clue you in.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Might be more sense changing the arguments to floats, maybe?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by quzah
    Well the location of the parenthesis might clue you in.

    Quzah.
    How so? I was referring to the float(x) style and not my suggested (float) x;

    The only thing that clues me in in the former is the fact that it is built-in type (float), but if it had been;

    something(x);

    Is that a function or a cast? Most would assume function, but it could be a cast if this was in the header:

    typedef float something;

    but (something)x is clearly a cast and not a function call

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 10:06 AM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM

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