Thread: Can someone tell me why this code isn't working please?

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    7

    Can someone tell me why this code isn't working please?

    EDIT!!

    It doesn't even compile

    it says:

    /tmp/ccgGoNoC.o: In function `hypo':
    hypotenuse.c.text+0x55): undefined reference to `sqrt'
    collect2: ld returned 1 exit status



    Code:
    #include <stdio.h>
    #include <math.h>
    
    double hypo( double side1, double side2 )
    
    {
    
            double answer;
    
            answer = sqrt( ( side1 * side1 ) + ( side2 * side2 ) );
    
            return answer;
    
    }
    
    int main ( int argc, char *argv[] )
    
    {
    
            double sidea, sideb;
    
            printf( "enter side1: \n" );
            scanf( "%f", &sidea );
    
            printf( "enter side2: \n" );
            scanf( "%f", &sideb );
    
            printf( "the hypotenuse is: %lf.\n", hypo( sidea, sideb ) );
    }
    Last edited by psynt555; 04-24-2012 at 10:41 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    $ gcc -Wall bar.c -lm
    bar.c: In function ‘main’:
    bar.c:23: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double *’
    bar.c:26: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double *’
    bar.c:29: warning: control reaches end of non-void function
    Try %lf instead.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    edited above, different problem has come up.

    it won't compile

    Code:
    /tmp/ccgGoNoC.o: In function `hypo':
    hypotenuse.c:(.text+0x55): undefined reference to `sqrt'
    collect2: ld returned 1 exit status
    any idea?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sounds like you need to link the math library, e.g., by passing -lm as a compiler option.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So I take it that you read NONE of my post.
    Not only did you fail to replace %f with %lf, you also missed the -lm as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    7

    Thankyou

    Quote Originally Posted by Salem View Post
    So I take it that you read NONE of my post.
    Not only did you fail to replace %f with %lf, you also missed the -lm as well.
    I tried the %lf thing and it didn't work, of course due to my compiling not being proper, so once I tried the compiling WITH your recommendation of using %lf instead of %f, it worked.

    So thankyou very much.

    SOLVED

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code working but not after 1 day
    By sheepilja in forum C Programming
    Replies: 3
    Last Post: 12-12-2010, 10:37 AM
  2. Code not working?
    By Elysia in forum C++ Programming
    Replies: 12
    Last Post: 04-06-2009, 01:57 AM
  3. Code Not working
    By srivatsan in forum C Programming
    Replies: 4
    Last Post: 10-13-2008, 04:38 AM
  4. working set code
    By chinu in forum C++ Programming
    Replies: 6
    Last Post: 04-13-2008, 08:44 PM
  5. Why is the above code working???
    By chottachatri in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2008, 07:29 AM