Thread: struct in function call

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    29

    struct in function call

    any idea why i get 0.0000 printed out for slength instead of my wanted value?
    Code:
    ...
    int main(void)
    {
        struct Segment seg;
        double seglength();
        double slength;
         
        do
        {
            /* request 1st point coordinates */
            printf("\nEnter first point coordinates x1 y1: ");
            scanf("%d" "%d" , &seg.x1 , &seg.y1);
            /* Flush keyboard buffer */
            fflush(stdin);
            /* request 2nd point coordinates */
            printf("\nEnter second point coordinates x2 y2: ");
            scanf("%d" "%d" , &seg.x2 , &seg.y2);
            /* Flush keyboard buffer */
            fflush(stdin);
            /* Print length of segment */
           printf("Segment length:%.5f" , slength);    
        } while(OKtoContinue());
        
        return 0;
    }
    
    double seglength(struct Segment seg)
     {
      double slength;
      slength = sqrt((seg.x2 - seg.x1) * (seg.x2 - seg.x1) + (seg.y2 - seg.y1) * (seg.y2 - seg.y1));
      return slength;
     }

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by Jasper View Post
    any idea why i get 0.0000 printed out for slength instead of my wanted value?
    Code:
    ...
    int main(void)
    {
        struct Segment seg;
        double seglength();
        double slength;
         
        do
        {
            /* request 1st point coordinates */
            printf("\nEnter first point coordinates x1 y1: ");
            scanf("%d" "%d" , &seg.x1 , &seg.y1);
            /* Flush keyboard buffer */
            fflush(stdin);
            /* request 2nd point coordinates */
            printf("\nEnter second point coordinates x2 y2: ");
            scanf("%d" "%d" , &seg.x2 , &seg.y2);        /* Flush keyboard buffer */
            fflush(stdin);
            /* Print length of segment */
           printf("Segment length:%.5f" , slength);    
        } while(OKtoContinue());
        
        return 0;
    }
    
    double seglength(struct Segment seg)
     {
      double slength;
      slength = sqrt((seg.x2 - seg.x1) * (seg.x2 - seg.x1) + (seg.y2 - seg.y1) * (seg.y2 - seg.y1));
      return slength;
     }
    Look at your scanf calls carefully. Where are you calling seglength by the way?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    your slength variable is not initialized and not updated anywhere

    fflush(stdin) is undefined - read FAQ
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM