How to combine hours and minutes?

closed account (zU5NwA7f)
Here is part of my code.

For example: The input is 200:

Hours : 3
Minutes : 20

how can i make them into 320?

1
2
3
4
5
6
7
   cout << "How long have you talked? " << endl;
    cin >> talk_time;
    
    
    hours = talk_time / 60;
    minutes = (static_cast<int>(talk_time) % 60);
   
int 3 hundred and 20 or 3:20?

3 hundred and 20, just multiply hours by 100 then add that to minutes
( 3 * 100 = 300 && minutes cannot be greater than 100 so that will work )

as for 3:20, for std::cout you could do

std::cout << hours << ':' << minutes;
3:20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
    constexpr int MINUTES_PER_HOUR = 60 ;

    // convert total minutes into hours, minutes
    int value = 200 ;
    const int hours = value / MINUTES_PER_HOUR ;
    const int minutes = value % MINUTES_PER_HOUR ;
    std::cout << hours << " hours and " << minutes << " minutes\n" ;

    // convert hours and minutes into total minutes
    const int total_minutes = hours * MINUTES_PER_HOUR + minutes ;
    std::cout << "total minutes: " << total_minutes << '\n' ;
}
closed account (zU5NwA7f)
JLBorges: Thank you for rely.
Paolettl301: Thank you for rely too

I have other question: How can i convert the military time into minutes and hour?

Since it like 1420? 0000? how can I spilt them in min and hour?
Last edited on
Divide by 1420 by 100 and you get 14. 1420 / 100 == 14
Take the remainder of the division and you get 20. 1420 % 100 == 20
closed account (zU5NwA7f)
about if it 0000 == 12:00 (Midnight)?
Last edited on
closed account (zU5NwA7f)
here is what I think for midnight.
1
2
3
4
5
if (time == 0)
{
  time +=12
}
if
{
//time is 0000
}
else
{
//calculate
}
Topic archived. No new replies allowed.