How do I make an itinerary file in C++?

Hello. I need to make an array of airports. The program needs to ask the user for a string of 3 letters for the airport code (e.g. SAN, or SFO), the latitude and longitude of each individual airport, the speed, and the amount of legs. The amount of airports equals the amount of legs plus one. Meaning it will have to ask for the information of how ever many legs there are plus another one. Then, it needs to calculate the distance between each individual airport, and the time it will need to go there. It will then need to write an html file from all of the information with a chart displaying the origin airport and destination airport, the distance between them, the time it will take, and the leg number for each individual leg. I don't know any html, but since this forum is for C++, I will only ask for help for the C++ part. HTML cant be that hard anyways. How do I make it ask all of the questions for each individual airport based on how many legs there are? How would it ask for all the details for each individual airport? Here is my code so far. I am required to use the stdafx. I am not required to have the structure, but I thought I would need it. Also, How do i reference to something inside the structure inside the function? I will also need help with writing to a file. Also, if the airport name is a string, which is an array of characters, how would I make an array of the airports when it is already an array? 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
42
43
44
45
46
47
48
49
50
51
52
  #include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "math.h"
#define MAX 4
#define PI 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200

double getDistance(double lat1, double long1, double lat2, double long2);
double deg2rad(double deg);
double calcTime(double Hours);

struct Airport {
	char Code[MAX];
	double Distance, Time, Lat, Long, Speed;
}Port, Port2;

int main()
{
	Airport Port1, Port2, Port3;
	int Legs;
	printf("Welcome aboard! \nThis program will show you an awesome graph of your trip's\n legs' Speed, Disatance, and the travel time\n");
	printf("\nHow many legs are there?\n");
	scanf("%d", &Legs);
	for (int i = 0; i < Legs + 1; i++) {

	}
	printf("Please enter your first airport 3 letter code");
	scanf("%s", Port1.Code);
	printf("Please enter the latitide for airport %s", Port1.Code);
	scanf("%lf", &Port1.Lat);
	printf("Please enter the longitude for airport %s", Port1.Code);
	scanf("%lf", &Port1.Long);
	printf("Please enter the ground speed you are travelling from airport %s", Port1.Code);
	scanf("%lf", &Port1.Lat);
    return 0;
}

double getDistance(double lat1, double long1, double lat2, double long2) {
	double ret;
	ret = acos(sin((deg2rad(lat1))) * sin(deg2rad(lat2)) + cos(deg2rad(lat1)) * cos(deg2rad(lat2)) * (cos(deg2rad(long1) - deg2rad(long2))));
	ret = ret * 10800 / PI;
	return ret;
}

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

double calcTime(double Hours) {
	Hours = Port1.Time;
	return (Port1.Distance / Port1.Time);
}
Last edited on
First off, if you're supposed to be writing this in C++, you should be using cin and cout, not printf/scanf.

Lines 2-4: The correct headers for C++ are <cstdio>, <cstring> and <cmath>.

Line 6: PI exceeds max digits for a double.

Line 14: Distance does not belong as a member variable. It is not an attribute of an Airport.

Line 15: What is the point of these two globals (port, port2). They're not used.

Line 19: Consider using an array of Airports, instead of individual variables.
1
2
  static const int MAX_LEGS = 3;
  Airport ports[MAX_LEGS+1];


Lines 24-26: Your for loop doesn't do anything.
Your for loop should look something like this:
24
25
26
27
28
29
30
31
32
33
34
35
	for (int i = 0; i < Legs + 1; i++) 
	{   cout << "Please enter airport code " << i+1 << ": "; 
        cin >> ports[i].Code;    
        cout << "Please enter the latitide for airport " << ports[i].Code << ": ";
	    cin >> ports[i].Lat;
	    cout << "Please enter the longitude for airport " << ports[i].Code << ": ";
	    cin >> ports[i].Long;
	    if (i != Legs)
	    {   cout << "Please enter the ground speed you are travelling from airport " << ports[i].Code << ": ";
	        cin >> ports[i].Speed;
	    }
	}


Line 50: Why are you assigning Port1.Time to an argument passed by value? The value will be lost when the function exists. calcTime should calculate the time between two Airports.

Line 51: You should be dividing by Speed, not time.


Last edited on
Hi AbstractionAnon. The reason I am using them is because that is what my professor wants. It is how he has taught us, and how I know how to. Your way is probably better. I got help and have made it so it successfully calculates the time and distance. All I need to do now is to write it to a file. How do I do that? Here is my code. It works, but I just need to know how to write everything to a file in HTML. How do I make it save in the HTML format? 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "math.h"
#define MAX 4
#define PI 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196442881097566593344612847564823378678316527120190914564856692346034861045432664821339360726024914127372458700660631558817488152092096282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179310511854807446237996274956735188575272489122793818301194912983367336244065664308602139494639522473719070217986094370277053921717629317675238467481846766940513200

double getDistance(double lat1, double long1, double lat2, double long2);
double deg2rad(double deg);

struct Airport {
	char Code[MAX];
	double Lat, Long, Speed;
}Port, Port2;

struct Leg {
	double Distance, Time;
};

int main()
{
	
	int Legs;
	printf("Welcome aboard! \n\nThis program will show you an awesome graph of your trip's\n legs' Speed, Disatance, and the travel time\n");
	printf("\nHow many legs are there?\n");
	scanf("%d", &Legs);
	Airport *ports = new Airport[Legs +1];
	for (int i = 0; i < Legs + 1; i++) {
		printf("Please enter airport %d three letter code >", i+1);
		scanf("%s", ports[i].Code);
		printf("Please enter the latitide for airport %s >", ports[i].Code);
		scanf("%lf", &ports[i].Lat);
		printf("Please enter the longitude for airport %s >", ports[i].Code);
		scanf("%lf", &ports[i].Long);
		printf("Please enter the ground speed you are travelling from airport %s >", ports[i].Code);
		scanf("%lf", &ports[i].Speed);
	}
	Leg *flight = new Leg[Legs];
	for (int i = 0; i < Legs; i++) {
		flight[i].Distance = getDistance(ports[i].Lat, ports[i].Long, ports[i + 1].Lat, ports[i + 1].Long);
		flight[i].Time = flight[i].Distance / ports[i].Speed;
		printf("distance airport is %f", flight[i].Time);
	}

    return 0;
}

double getDistance(double lat1, double long1, double lat2, double long2) {
	double ret;
	ret = acos(sin((deg2rad(lat1))) * sin(deg2rad(lat2)) + cos(deg2rad(lat1)) * cos(deg2rad(lat2)) * (cos(deg2rad(long1) - deg2rad(long2))));
	ret = ret * 10800 / PI;
	return ret;
}

double deg2rad(double deg) {
	return (deg * PI / 180);
}
Last edited on
Line 14: You still have two unused globals (port1, ports2). Globals should be avoided. These variables should be removed.

Line 35-36: Asking for the speed from the destination airport makes no sense. This is why I had an if statement at line 31 of the snippet I posted.

As for writing an HTML file, C++ does not provide any native support for HTML. There are open source libraries that do provide classes for HTML. However, for your assignment, all you really need to do is open a text file and write some HTML to it.

1
2
3
4
5
6
7
8
  ofstream htmlfile ("myfile.html");
  if (! htmlfile.is_open())
  {  // file did not open 
  }
  htmlfile << "<HTML>" << endl;
  //  output what ever HTML statements you want
  htmlfile << "</HTML>" << endl;
  htmlfile.close();



Last edited on
Topic archived. No new replies allowed.