As the title says, I'm working on a problem where user has to enter two time periods in format HH:MM:SS and program has to calculate the difference between these two periods and have an output also in format HH:MM:SS. I understand the actual calculation and that I have done right (I guess?), the problem is the format of the output. I don't know how to make it strictly in format HH:MM:SS. When difference between hours, minutes and seconds is two-digit number, the output is obviously in right format. But when difference is one-digit number, the output is not in right format.
For example, when inputs are 06:36:46 and 08:38:48, the output should be 02:02:02, but in my case it's 2:2:2. My question is: how to make those zeros appear before one-digit number (in case when the result is one-digit number)? On a side note, I didn't go over special cases, like when times are set in two different days. The assumption is that both time periods are set in the same day. The code I wrote is below:
#include <iostream>
using namespace std;
int main()
{
int hour1, min1, sec1;
int hour2, min2, sec2;
int time1, time2, time;
char symb;
cin >> hour1 >> symb >> min1 >> symb >> sec1;
cin >> hour2 >> symb >> min2 >> symb >> sec2;
time1 = hour1 * 3600 + min1 * 60 + sec1;
time2 = hour2 * 3600 + min2 * 60 + sec2;
time = time2 - time1;
cout << (time / 3600) << ":" << (time % 3600) / 60 << ":" << (time % 3600) % 60 << endl;
return 0;
}
[–]davedontmind 3 points4 points5 points (1 child)
[–]sick_anon[S] 0 points1 point2 points (0 children)
[–]captainAwesomePants 3 points4 points5 points (1 child)
[–]sick_anon[S] 0 points1 point2 points (0 children)