How to calculate distance in Nautical miles using structures

Hello. I am trying to make a program where I need to calculate the distance between two airports using a structure. The program needs to ask the user to enter a code such as "SAN" for the San Diego airport, then ask for its coordinates, elevation, and magnetic variation. Then it will need to ask the same for the the same for a different airport. I need to have a function inside of the struct called getDistance that uses a formula we made earlier to calculate the Nautical miles. There needs to be another formula inside of the structure called getDistance2 that only takes one airport as a parameter and breaks down the elements of the airports and call the getDistance to calculate the nautical miles. I made a structure where I put in a formula that I made earlier. I tested it with the printf to see if it will output the right number, but it gives me a completely different number. I am also trying to use a string for the code of the airport, but can't seem to get it to work with scanf. I am required to use the stdafx.h. I would really appreciate it if you can show me how to fix the structure and fix it so I can make the program ask the user for the code of the airport. Also, how would I ask for the second airport? Thank you.

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
  	// Distance Structures Project.cpp : Defines the entry point for the console application.
	//

	#include "stdafx.h"
	#include <string.h>
	#include "math.h"
	#define MAX 4
	#define PI 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248
	double deg2rad(double deg);
	typedef struct {
		double Lat1, Long1, Lat2, Long2, Answer, Elevation, Mvar;
		char Code[MAX];
		double getDistance(double LA1, double LO1, double LA2, double LO2, double ret) {
			ret = acos(sin((deg2rad(LA1))) * sin(deg2rad(LA2)) + cos(deg2rad(LA1)) * cos(deg2rad(LA2)) * (cos(deg2rad(LO1) - deg2rad(LO2))));
			ret = ret * 10800 / PI;
			return ret;
		}
	}Airport;


	int main()
	{	
		struct Airport;
	printf("Please enter code of airport 1 >");
	scanf("%s", Code);
	printf("Please enter latitude of airport %s >", Code);
	scanf("%d", Code);
	printf("Please enter longitude of airport %s >", Code);
	scanf("%d", Code[]);
	printf("Please enter magnetic variation of airport %s >", Code);
	scanf("%d", Code[]);
	printf("Please enter elevation of airport %s >", Code);
	scanf("%d", Code[]);
		return 0;
	}

	double deg2rad(double deg) {
		return (deg * PI / 180);
	}



Please go to my bottom reply where I fixed some things
Last edited on
is there any reason you are using c functions instead of c++ functions?
Hello. Thank you for your reply. It is what I am told to do by my professor.
Hello Bdanielz or anyone else. I managed to fix it so it will ask the user for the details about the 2 different airports. Now how I use the function inside the structure to use the formula I have to calculate the Nautical miles between the two points? I also need to make another function inside the structure to use that function called getDistance2 that only takes one airport as a parameter and breaks down the elements of the airports and call the getDistance to calculate the nautical miles. Thank you. Here is the new code.

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
51
52
53
54
	// Distance Structures Project.cpp : Defines the entry point for the console application.
	//

	#include "stdafx.h"
	#include <string.h>
	#include "math.h"
	#define MAX 4
	#define PI 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248
	double deg2rad(double deg);
	double getDistance(double LA1, double LO1, double LA2, double LO2, double ret);
	struct Airport {
		double Lat, Long, Answer, Elevation, Mvar;
		char Code[MAX];
		double getDistance(double LA1, double LO1, double LA2, double LO2, double ret) {
			ret = acos(sin((deg2rad(LA1))) * sin(deg2rad(LA2)) + cos(deg2rad(LA1)) * cos(deg2rad(LA2)) * (cos(deg2rad(LO1) - deg2rad(LO2))));
			ret = ret * 10800 / PI;
			return ret;
		}
	}Port1, Port2;


	int main()
	{	
		double answer;
	printf("Please enter code of airport 1 >");
	scanf("%s", Port1.Code);
	printf("Please enter latitude of airport %s >", Port1.Code);
	scanf("%lf", &Port1.Lat); // It goes wrong after here.
	printf("Please enter longitude of airport %s >", Port1.Code);
	scanf("%lf", &Port1.Long);
	printf("Please enter magnetic variation of airport %s >", Port1.Code);
	scanf("%lf", &Port1.Mvar);
	printf("Please enter elevation of airport %s >", Port1.Code);
	scanf("%lf", &Port1.Elevation);
	//Second airport
	printf("Please enter code of airport 2 >");
	scanf("%s", Port2.Code);
	printf("Please enter latitude of airport %s >", Port2.Code);
	scanf("%lf", &Port2.Lat); 
	printf("Please enter longitude of airport %s >", Port2.Code);
	scanf("%lf", &Port2.Long);
	printf("Please enter magnetic variation of airport %s >", Port2.Code);
	scanf("%lf", &Port2.Mvar);
	printf("Please enter elevation of airport %s >", Port2.Code);
	scanf("%lf", &Port2.Elevation);
	answer = (double Port1.getDistance(answer));
		return 0;
	}

	double deg2rad(double deg) {
		return (deg * PI / 180);
	}

Something like this? You still need to code the arithmetic.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct Airport {
    double Lat, Long, Answer, Elevation, Mvar;
    char Code[MAX];

    double getDistance(double LA1, double LO1, double LA2, double LO2, double ret) {
        ret = acos(sin((deg2rad(LA1))) * sin(deg2rad(LA2)) +
                   cos(deg2rad(LA1)) * cos(deg2rad(LA2)) * (cos(deg2rad(LO1) - deg2rad(LO2))));
        ret = ret * 10800 / PI;
        return ret;
    }

    double getDistance_airport(Airport const airport) {

        printf("You passed in airport %s \n", airport.Code);


        return .0;
    }
} Port1, Port2;
Topic archived. No new replies allowed.