CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2016
    Posts
    6

    How to calculate Nautical miles between two airport coordinates with a structure?

    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.

    Code:
    // 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);
    	}

  2. #2
    Join Date
    Nov 2016
    Posts
    6

    Re: How to calculate Nautical miles between two airport coordinates with a structure?

    Hello. 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.

    Code:
    	// 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);
    	}
    Last edited by 2kaud; November 12th, 2016 at 09:48 AM.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to calculate Nautical miles between two airport coordinates with a structure?

    Are you using c or c++ - the code refers to .cpp (which is c++), but your code looks c?

    Why is the function getdistance() part of the struct? It needs 2 sets of latitude and longitude for it to calculate the answer - so needs these to be passed as arguments. Also, why is ret a parameter when it's not needed?

    Don't you need something like this
    Code:
    struct Airport {
    	double Lat, Long, Answer, Elevation, Mvar;
    	char Code[MAX];
    }Port1, Port2;
    ...
    double getDistance(double LA1, double LO1, double LA2, double LO2) {
    	double 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;
    }
    ....
    printf("distance is: %lf\n", getDistance(Port1.Lat, Port1,Long, Port2.Lat, Port2.Long));
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Nov 2016
    Posts
    6

    Re: How to calculate Nautical miles between two airport coordinates with a structure?

    Quote Originally Posted by 2kaud View Post
    Are you using c or c++ - the code refers to .cpp (which is c++), but your code looks c?

    Why is the function getdistance() part of the struct? It needs 2 sets of latitude and longitude for it to calculate the answer - so needs these to be passed as arguments. Also, why is ret a parameter when it's not needed?

    Don't you need something like this
    Code:
    struct Airport {
    	double Lat, Long, Answer, Elevation, Mvar;
    	char Code[MAX];
    }Port1, Port2;
    ...
    double getDistance(double LA1, double LO1, double LA2, double LO2) {
    	double 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;
    }
    ....
    printf("distance is: %lf\n", getDistance(Port1.Lat, Port1,Long, Port2.Lat, Port2.Long));



    Hello. Thank you for your reply. I would think I need something like that, but my professor's instructions is to put the functions in the struct. I would not recognize that my code is C since C++ is my first coding language. This is just how my professor has taught us to code. He won't accept it any other way. Would I not be able to make a set of Latitude and Longitude for the Port1, and Port2? I mean have one pair of coordinated equal the Port1. Then the other with Port2. Obviously, I could be wrong. Thank you again for your help.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to calculate Nautical miles between two airport coordinates with a structure?

    Then you need something like this
    Code:
    struct Airport {
    	double Lat, Long, Answer, Elevation, Mvar;
    	char Code[MAX];
    
    	double getDistance(double LA1, double LO1) {
            	double ret = acos(sin((deg2rad(LA1))) * sin(deg2rad(Lat)) + cos(deg2rad(LA1)) * cos(deg2rad(Lat)) * (cos(deg2rad(LO1) - deg2rad(Long))));
    		ret = ret * 10800 / PI;
    		return ret;
    	}
    }Port1, Port2;
    ....
    printf("distance is: %lf\n", Port1.getDistance(Port2.Lat, Port2.Long));
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured