C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-09-2006, 08:31 PM   #1
Registered User
 
Join Date: Sep 2005
Posts: 10
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.
radiohead is offline   Reply With Quote
Old 01-09-2006, 08:41 PM   #2
Registered User
 
Join Date: Apr 2005
Posts: 134
change this

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

Code:
sscanf(line, "%f", &x[0]);
nkhambal is offline   Reply With Quote
Old 01-09-2006, 08:48 PM   #3
Registered User
 
Join Date: Sep 2005
Posts: 10
ah, thanks
radiohead is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:52 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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