Reputation:
I am looking for help in C++ converting an integer input into 24 hours format. Suppose you build a scheduled time for airplanes from A (start) to B (end); The user is able to entry 4 different variables, such as the journey time of the airplane from A to B; the first airplane time; the last airplane time; and the frequency of traveling in one day.
The input would be like the following:
int journey_time;
int start_time;
int end_time;
int frequency;
cout << "Please enter your journey time:\n";
cin >> journey_time;
cout << "Please enter your start time:\n";
cin >> start_time;
cout << "Please enter your end time: \n";
cin >> end_time;
cout << "Please enter the frequency: \n";
cin >> frequency;
IMPORTANT to mention is that the user should type in the times as integer and the hours should be transformed in a 24 hour clock times And this is where I am stucked; I was able to transform the integer into minutes but not into hours
so far I have this code
for (int total_min_start= start_time; total_min_start <= end_time; total_min_start +=frequency)
{
hs = total_min_start/100; // to get the hours
ms = total_min_start %100; // to get the minutes
x = (hs *60) + ms; // converted into minutes
h = ((x-ms)/60) * 100; <= this is where I am confused, I want here the reconversion of hours into to 24 hours format
t = h + ms; // this is the total time after reconversion
cout << t << "\t" << t + journey_time<< endl;
}}}
if I type in for journey time=15; start_time=0930; end_time= 1000 and frequency = 15 min, i get the following output (left) but I want this output (right)
WRONG OUTPUT RIGHT OUTPUT
930 945 930 945
945 960 945 1000
960 1000 1000 1015
I would be very very thankful for any kind of help or hint... Thanks in advance
Upvotes: 0
Views: 1054
Reputation: 1691
Create separate functions to convert minutes into 24 hour time format and vice versa.
auto get_24hr = [](int minutes){ return (minutes/60)*100 + (minutes%60);};
auto get_minutes = [](int time_24){return (time_24/100)*60+ (time_24 % 100);};
for (int total_min_start= start_time; total_min_start <= end_time; total_min_start +=frequency)
{
auto x = get_minutes(total_min_start); // converted into minutes
cout << get_24hr(x) << "\t" << get_24hr(x+journey_time) << endl;
}
Upvotes: 0
Reputation: 409176
Okay lets unroll the loop and do each step separately.
First iteration:
// Loop initialization
total_min_start = 930;
hs = 930 / 100; // 9
ms = 930 % 100; // 30
x = (9 * 60) + 30; // 540 + 30 = 570
h = ((570 - 30) / 60) * 100; // (540 / 60) * 100 = 9 * 100 = 900
t = 900 + 30; // 930
// t + journey_time = 930 + 15 = 945
total_min_start += 15; // 945
So far so good, so we start the second iteration:
total_min_start = 945;
hs = 945 / 100; // 9
ms = 945 % 100; // 45
x = (9 * 60) + 45; // 540 + 45 = 585
h = ((585 - 45) / 60) * 100; // (540 / 60) * 100 = 9 * 100 = 900
t = 900 + 45; // 945
// t + journey_time = 945 + 15 = 960
total_min_start += 15; // 960
Ah here it seems we have a problem. The value in t
is not the time in minutes, it's actually two separate values: Hours and minutes.
There are two ways to solve this: As soon as the user have entered the input, separate it into the two values, and keep track of the time in two separate variables.
The second way to solve it, and which I think is much easier, is to immediately after input convert the input into a single value, whose unit is the number of minutes after midnight. That is, if the user inputs 930
meaning 9:30 AM, you convert it into the value 570 (which is the number of minutes after midnight). Then work only with that value, increase and decrease as you wish, and only convert it back to your preferred format when presenting it to the user.
To help you with the conversions back and forth I suggest you have a couple of utility or helper functions: One for converting the timestamp into minutes past midnight, and one for converting the number of minutes past midnight into the timestamp. Then you could easily do something something like
cout << minutes_to_time(t) << '\t' << minutes_to_time(t + journey_time) << '\n';
Oh, and don't forget to test for wrapping around at midnight. Both ending up with t
being exactly midnight, with t + journey_time
being midnight, and with t
being a little before midnight and t + journey_time
being a little after midnight.
Upvotes: 1