C++ Help Lat/Long Distance

I need help/feedback on where i'm messing up on. (I'm still new to c++) The input i'm using is SAN to JFK
Anything helps much appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.14159
#define R (10800 / PI) 
#define To_Rad (PI / 180)




    





int main(int argc, const char * argv[]) {
    //Variable Declaration
    double Lat1, Lat2, Long1, Long2, Degrees, Radians, iNauticalMiles, Distance;
    
    //User Input
    printf("Please enter location one's Latitude : ");
    scanf("%lf", &Lat1);
    printf("Please enter location one's Longitude : ");
    scanf("%lf", &Long1);
    printf("Please enter location two's Longitude : ");
    scanf("%lf", &Lat2);
    printf("Please enter location two's Latitude : ");
    scanf("%lf", &Long2);
    
    //Calculations
    Degrees = acos(sin(Lat1) * sin(Lat2) + cos(Lat1) * cos(Lat2) * cos(Long1 - Long2));
    Distance = (R * Degrees);
    
    //Convert To Radians
    Radians = (Degrees * To_Rad);
    
    //Convert to Statute Miles
    iNauticalMiles = (Radians * 1.15);
    
    //Output
    printf("Your Distance is %.2lf miles \n" ,iNauticalMiles);
    
    
    return 0;
}

 
Last edited on
The first thing I notice is that your prompts indicate you are reading in location one's ordered pair then location two's ordered pair, but the scanf() statements themselves seem to indicate you are reading the latitudes followed by the longitudes. Which way did you mean to do it?
Zhuge,
oh, Thanks for pointing that out. (I meant to have lat then long). Damn copy paste gets me everytime. no more of that.
heres the coordanites i'm working with atm :
SAN indicates San Diego, and its coordinates are Lat = 32.7335556 and Long = -117.1896667. JFK is New York and its coordinates are Lat = 40.6397511, Long = -73.7789256.

i'm still not getting the right outcome.

Last edited on
The trigonometric functions in the standard library take their arguments as and return radian values; so you'll need to convert those degree inputs before sending them in. On the other hand, you don't need to convert the result of the function to radians as it is already radians.

Also, you don't seem to use the variable Distance anywhere, so I'd remove it unless that was a mistake.
Topic archived. No new replies allowed.