calculate the difference between two time periods

how to calculate the difference between the departure and arrival times of a plane entered from the console?

in my task it is said to display data on aircrafts with the highest average speed (structure array i believe) ; time of arrival, departure, distance, etc. are entered manually from the console.

what's the best way to calculate flight times for this? some function? can you explain please


also, here's the full task just so you get the idea of what i need:

"Flight data includes flight number, departure time, arrival time, direction, aircraft model, distance.

Organize an information array for storing data in the form of a given structure and fill it with data from the keyboard (console). After entering the data on the current flight, allow you to correct the information as needed, if the user has such a need.

Display data on flights with the maximum average speed.

Display flight data from a user-specified destination.

Display data on flights departing from a given point within a given period of time."


ps: just a piece of my code, don't know if you need it


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>
#include <string>
#include<ctime>
using namespace std;
 
struct Time 
{
    short hours;
    short minutes;
};
 
struct flight
{
    int num; // номер рейса
    Time departure; // отправление
    Time arrival; // прибытие
    string direction; // направление
    char model; // модель
    double distance; // расстояние
};
Last edited on
So long as it arrives on the same day it departs, the time taken in minutes is:

(arrivalTime.hours * 60 + arrivalTime.minutes) - (departureTime.hours * 60 + departureTime.minutes)

The logic here is to turn the times into a number of minutes from midnight, and then simply subtract one from the other.

For any given time, how many minutes past midnight is it? Well, it's the number-of-hours part of the time multipied by sixty, and then add the number-of-minutes part of the time.
Hello laura fidarova,

The code is a good start, so if you have not written the code for getting the input and saving it into the structs work on that first. To go along with that you could write the code to print the structs. If you have done this already then never mind.

A note: with using line 4 calling the struct "Time" may not be the best choice. It is possible that "Time" may conflict with something defined in the "time.h" header file. It has been awhile and I really do not remember.

I do like Repeater's approach of working in minutes as the way of using time, but as he said crossing midnight is something to consider along with time zones. A short 1 hour flight could take off at 10:00 and land at 10:00, (24 hour format), because the plain crossed into a time zone that is 1 hour behind.

I do not know if any of this applies? You may just need to work with time and distance and from that get th speed when needed.

Andy
thank you so much! i really appreciate your help!:)
Repeater, how do i actualize it? i'm a little confused, i don't know where or how to correctly put it
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
#include <iostream>
#include <string>
#include<ctime>
using namespace std;

struct Time
{
    short hours;
    short minutes;
};

struct flight
{
    int num; // номер рейса
    Time departure; // отправление
    Time arrival; // прибытие
    string direction; // направление
    string model; // модель
    double distance; // расстояние
};

int main()
{
    const int plane = 5;
    flight info[plane] = {};

    for (int i = 0; i < plane; i++)
    {
        cout << "Flight number: ";
        cin >> info[i].num;

        cout << "Departure time: ";
        cin >> info[i].departure.hours >> info[i].departure.minutes;

        cout << "Arrival time: ";
        cin >> info[i].arrival.hours >> info[i].arrival.minutes;

        cout << "Direction: ";
        cin >> info[i].direction;

        cout << "Model: ";
        cin >> info[i].model;

        cout << "Distance: ";
        cin >> info[i].distance;

        cout << endl;
    }

    return 0;
}
Last edited on
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
struct hm_time
{
    int hours = 0 ;
    int minutes = 0 ;

    static constexpr int MINS_PER_HOUR = 60 ;
    static constexpr int HOURS_PER_DAY = 24 ;
    static constexpr int MINS_PER_DAY = MINS_PER_HOUR * HOURS_PER_DAY ;
};

int minutes_since_midnight( hm_time tm )
{
    return tm.hours * hm_time::MINS_PER_HOUR + tm.minutes ;
}

int diff_in_minutes( hm_time from, hm_time to )
{
    auto diff = minutes_since_midnight(to) - minutes_since_midnight(from) ;
    
    // adjust diff if time to is on the next day after time from
    // we assume that the two times would not differ by more than one day 
    if( diff < 0 ) diff += hm_time::MINS_PER_DAY ;

    return diff ;
}
Topic archived. No new replies allowed.