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
|
#include <iostream>
unsigned int to_secs_since_midnight( unsigned int hh, unsigned int mm, unsigned int ss )
{ return hh*3600 + mm*60 + ss ; }
void from_secs_since_midnight( unsigned int total_secs, unsigned int& hh, unsigned int& mm, unsigned int& ss )
{
hh = ( total_secs / 3600 ) % 24 ; // %24 to handle roll over to the next day
mm = ( total_secs / 60 ) % 60 ;
ss = total_secs % 60 ;
}
unsigned int get_secs_from_hh_mm_ss( const char* prompt )
{
unsigned int hh, mm, ss ;
char sep ;
std::cout << prompt << " as hh:mm:ss " ;
std::cin >> hh >> sep >> mm >> sep >> ss ; // input error handling elided for brevity
std::cout << hh << ':' << mm << ':' << ss << '\n' ; // for testing; may be commented out
return to_secs_since_midnight( hh, mm, ss ) ;
}
int main()
{
const unsigned int start_secs = get_secs_from_hh_mm_ss( "give me a start time " ) ;
const unsigned int incr_secs = get_secs_from_hh_mm_ss( "give me the increment" ) ;
const unsigned int end_secs = start_secs + incr_secs ;
unsigned int hh, mm, ss ;
from_secs_since_midnight( end_secs, hh, mm, ss ) ;
std::cout << "\nend time is: " << hh << ':' << mm << ':' << ss << '\n' ;
}
|