Segfaulting Distance Program

This is a discussion on Segfaulting Distance Program within the C Programming forums, part of the General Programming Boards category; I got a book that one of the excersises is to write a program to tell the distance of something. ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    8

    Segfaulting Distance Program

    I got a book that one of the excersises is to write a program to tell the distance of something. I have the program written, but it segfaults where I put the comment.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(int argc, char *argv[])
    {
        float distance;   //total distance
        float x[2];          //the x coordinates
        float y[2];          //the y coordinates
        char line[100];  //the read line
        
        printf("What is x1?\n");
        fgets(line, sizeof(line), stdin);
        sscanf(line, "%f", x[0]);
    
       /*It segfaults here*/
    
        printf("\n\nWhat is x2?\n");
        fgets(line, sizeof(line), stdin);
        sscanf(line, "%f", x[1]);
        
        printf("\n\nWhat is y1?\n");
        fgets(line, sizeof(line), stdin);
        sscanf(line, "%f", y[0]);
        
        printf("\n\nWhat is y2?\n");
        fgets(line, sizeof(line), stdin);
        sscanf(line, "%f", y[1]);
    
        distance = sqrt(((x[1] - x[0]) * (x[1] - x[0])) + ((y[1] - y[0]) * (y[1] - y[0])));
        
        printf("\n%f", distance);
      
      system("PAUSE");	
      return 0;
    }
    Any help is greatly appreciated.

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    change this

    Code:
    sscanf(line, "%f", x[0]);
    to

    Code:
    sscanf(line, "%f", &x[0]);

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    ah, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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