Convert seconds and minutes

Hello,
I am quite new to programming and I have a question.

I am writing a program where the user writes down their time for a lap they ran, and there were 2 laps.
Then the program calculates the total time it took for both laps.
Now my problem here is that lets say you ran in 0 hours, 43 minutes and 52 seconds.
The second lap you ran 40 minutes and 30 seconds.
This adds upp to
0 Hours
83 Minutes
82 Seconds

How do I convert any seconds above 60 to minute(s), and any minute above 60 to hour(s)?


1
2
3
4
5
6
7
8
9
10
11
12
int hours = 0;
int minutes = 83;
int seconds = 82;

minutes += seconds/60;
seconds %= 60;
hours += munutes/60;
minutes %= 60;

std::cout << hours;
std::cout << minutes;
std::cout << seconds;
1
24
22
After Adding up Seconds and Minutes, You can do it the following way:

1
2
3
4
5
6
7
8
9
int minutes = 83, seconds = 82, hours = 0;

minutes = minutes + (seconds/60)    //1.366 will round of to 1 because minutes is of type integer

seconds = seconds % 60    //82 mod 60 will leave 22 as remainder so seconds will be 22

hours = hours + (minutes/60)    //Same as above

minutes = minutes % 60


This is just an explanation Now try to make it General .....
Last edited on
Topic archived. No new replies allowed.