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.