Flight Times Help!

I wanna make sure I'm on the right track, and to have a little help! Thank you in advance.


Flight times in the United States can be estimated using a formula based on the direction of travel and
distance between airports. Regardless of the distance or direction, there is also a fixed amount of time
for the take-off and landing cycles. Needed information:
• It takes 0.2 hours for takeoff and climb-out from the origin airport.
• It takes 0.4 hours for approach and landing at the destination airport.
• The aircraft ground speed after climb-out when traveling west is 480 miles/hour.
• The aircraft ground speed after climb-out when traveling east is 540 miles/hour.
• The aircraft ground speed after climb-out when traveling north or south is 510 miles/hour.
Given this data, the flight time (FT) can be estimated as:
FT = 0.20 + 0.40 + (distance / groundSpeed);
Write a C++ program that asks the user for the distance traveled (0-6000) expressed in whole miles and
the flight direction as a character (N, S, W, E). For each flight entered output the approximate flight
time to the nearest 1/100 of an hour in a time format. Check for case and valid inputs; use constants as
needed. To pad numbers with leading zeros you need to insert a 0 into the output format-using the
setfill() manipulator.
For example: To travel from Dallas/Fort Worth to Chicago O’Hare. The distance is 801 miles and
the general direction of travel is north. The travel time can be computed as follows:
0.20 + 0.40 + (801 / 510) = 0.6 + 1.57 = 2.17 (two decimal places)
Finally, convert decimal answer to time. Format (HH:MM:SS):
2.17 = 02:10:12
Output should look similar to below.
Sample Runs:
Enter the distance traveled: 1883
Enter the direction: E
The estimated flight time is: 4.09 or 04:05:24
Enter the distance traveled: 1100
Enter the direction: S
The estimated flight time is: 2.76 or 02:45:36

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
  #include <iostream>

using namespace std;

int main()
{

	int miles;
	char FlightDirection;
	double FlightTime = 0;
	double hours, minutes, seconds;
	double decMinutes;
	int groundSpeed;


	cout << "Enter the distance traveled: "; cin >> miles;
	cout << "Enter the direction: "; cin >> FlightDirection;

	

	if (FlightDirection == W)
	{

		groundSpeed = 480;

	}
	else if (FlightDirection == E)
	{

		groundSpeed = 540;

	}
	else
	{

		groundSpeed = 510;

	}

	FlightTime = 0.20 + 0.40 + (miles / groundSpeed);
	hours = floor(FlightTime);

	decMinutes = ((ceil(FlightTime / .01) / 100) - hours) * 60;

	minutes = floor(decMinutes);

	seconds = (decMinutes - minutes) * 60.0;


	cout << "The estimated flight time is :";
	cout << decMinutes << " or ";
	cout << hours << ":" << minutes << ":" << seconds << endl;

	cin.get();

	return 0;
}
We can't figure out the problem you are having yet.
Hello Unisaurus,

While playing with your program I had a problem with printing the seconds. Using 1100 miles and dir of S I got the flight time of 2.6 which should be 02:36:00, but the seconds came out as 4.26326e-13. It turns out that decMinutes had the number 36.000000000000007. So the calculation for seconds was 0.000000000000007 * 60 when it should be 0 * 60. I fixed the problem with seconds = (floor(decMinutes) - minutes) * 60.0;. Sometimes floats and doubles do not always have a zero decimal value.

Based on instructions I used this to format the output.
cout << setw(2) << setfill('0') << hours << ":" << setw(2) << setfill('0') << minutes << ":" << setw(2) << setfill('0') << seconds << endl;.

Hope that helps,

Andy
Thanks Andy that helped alot!
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{

	do
	{

		int miles;
		char FlightDirection;
		double FlightTime = 0.0;
		double hours, minutes, seconds;
		double decMinutes;
		double groundSpeed = 0.0;


		cout << "\nEnter the distance traveled: ";
		cin >> miles;
		cout << "Enter the direction: ";
		cin >> FlightDirection;


		if (FlightDirection == 'w' || FlightDirection == 'W')
		{

			groundSpeed = 480;

		}
		else if (FlightDirection == 'E' || FlightDirection == 'e')
		{

			groundSpeed = 540;

		}
		else
		{

			groundSpeed = 510;

		}




		FlightTime = 0.20 + 0.40 + (miles / groundSpeed);

		hours = floor(FlightTime);

		decMinutes = ((ceil(FlightTime / .01) / 100) - hours) * 60;

		minutes = floor(decMinutes);

		seconds = (decMinutes - minutes) * 60.0;



		cout << "\nThe estimated flight time is: ";
		cout << setprecision(3) << setfill('0') << FlightTime << " or ";
		cout << setw(2) << setfill('0') << hours << ":";
		cout << setw(2) << setfill('0') << minutes << ":";
		cout << setw(2) << setfill('0') << seconds << endl;
	} 
	while (1 != 0);




	cin.ignore();
	cin.get();

	return 0;
}


Here is the final code.
Topic archived. No new replies allowed.