### Thread: Can anyone see what is wrong with this code?

1. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2002
Posts
1
Rep Power
0

#### Can anyone see what is wrong with this code?

What's up, :)

I'm writing a program to calculate the distance between two latitude/longitude coordinates. the below code is what I wrote. It is not working for some reason and I'm clueless to what is wrong. Maby you can catch something that I'm missing. You have any ideas? Any help would be great. Thanks in advance! :)

Peace
Wise1

Code:
```#include<stdio.h>
#include<math.h>
#include<conio.h>

main(){
//Declarations
double pi=3.14159265358979,a,c,d,dlat,dlon,lat1,lat2,lon1,lon2;
int days,hours,s,r=3956;

printf("Origin Coordinates \n  1) 16, 64 \t\t Speed: 20 mph \n  2) 32, 64 (Bermuda) \t Speed: 10 mph \n\nDestination Coordinates \n  1) 26, 80 (Ft. Lauderdale) \n\n\n");

printf("Please Enter the Origin Coordinates: ");
scanf("%f, %f",&lat1,&lon1);
printf("Please Enter the Destination Coordinates: ");
scanf("%f, %f",&lat2,&lon2);
printf("Please Enter the Speed of Travel: ");
scanf("%d",&s);

//Convert Coordinates From Decimal Degrees to Radians
lat1*=pi/180;
lat2*=pi/180;
lon1*=pi/180;
lon2*=pi/180;

//The Haversine Formula
dlat=lat2-lat1;
dlon=lon2-lon1;
a=((sin(dlat/2))*(sin(dlat/2)))+cos(lat1)*cos(lat2)*((sin(dlon/2))*(sin(dlon/2)));
c=2*atan2(sqrt(a),sqrt(1-a));
d=r*c;

//Find Time
hours=d/s;
days=hours/24;
hours-=days*24;

//Print Output
printf("\n\nDistance: %f Statute Miles \nSpeed: %d mph\nTime (Days/Hours): %d/%d",d,s,days,hours);

getch();
}```
2. Perhaps you can be a little more informative. What is the problem(s) you are having with this code?
Have you tried running it through a debugger to watch the values and/or stack?
3. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2002
Posts
1
Rep Power
0
What's up Onslaught, :)

The problem is that I'm getting all zeros for my results except for the speed. For some reasons the doubles are not working with scanf(). I know the eqation works corectly because I tested it with defined variables but once I asked for user input I got all zeros. Dose that help. :)

Peace
Wise1
4. Yes, that helps greatly.
Have you tried breaking the scanfs for the coordinate up into two seperate lines?
i.e.
Code:
```printf("\nPlease Enter the Origin Coordinates:\n");
printf("Lat: ");
scanf("%f", &lat1);
printf("\nLon: ");
scanf("%f", &lon1);
printf("Lat: ");
scanf("%f", &lat1);
printf("\nLon: ");
scanf("%f", &lon1);```
edit: fix a typo
5. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2002
Posts
1
Rep Power
0
What's up Onslaught, :)

Yes, I tried that. It does not work. :(

Peace
Wise1
6. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2002
Posts
1
Rep Power
0
What's up, :)

nevermind I figured it out. I had to use %lf to scan it instead of %f. Thanks for your help anyways. :)

Peace
Wise1