#include <iostream>
#include <string>
struct timestamp {
int hour, min, sec;
} ;
// overload handles the prepend-0 issue
std:: ostream & operator<< ( std:: ostream & stream, const timestamp& ts ) {
return stream
<< ( ts.hour < 10 ? "0" : "" ) << ts.hour << ":"
<< ( ts.min < 10 ? "0" : "" ) << ts.min << ":"
<< ( ts.sec < 10 ? "0" : "" ) << ts.sec ;
}
timestamp modify( const timestamp& ts, const std:: string & name ) {
std:: cout << name << "'s starting value is: " << ts << "\n Would you like to modify it? [y/n] " ;
std:: cout .flush ( ) ;
char response;
std:: cin >> response;
// If no modification; return original
if ( ! ( response == 'y' || response == 'Y' ) )
return ts;
int hour, min, sec;
hour = min = sec = - 1 ;
while ( hour < 0 || hour > 23 ) {
std:: cout << "Please enter hour [0-23]: " ;
std:: cout .flush ( ) ;
std:: cin >> hour;
}
while ( min < 0 || min > 59 ) {
std:: cout << "Please enter minutes [0-59]: " ;
std:: cout .flush ( ) ;
std:: cin >> min;
}
while ( sec < 0 || sec > 59 ) {
std:: cout << "Please enter seconds [0-59]: " ;
std:: cout .flush ( ) ;
std:: cin >> sec;
}
return timestamp { hour, min, sec } ;
}
int main( ) {
timestamp t1 { 20 , 30 , 45 } ;
timestamp t2 { 19 , 25 , 55 } ;
t1 = modify( t1, "Time1" ) ;
t2 = modify( t2, "Time2" ) ;
std:: cout << "Modified:\n " << t1 << "\n " << t2 << std:: endl ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgoKc3RydWN0IHRpbWVzdGFtcCB7CiAgICBpbnQgaG91ciwgbWluLCBzZWM7Cn07CgoKLy8gb3ZlcmxvYWQgaGFuZGxlcyB0aGUgcHJlcGVuZC0wIGlzc3VlCnN0ZDo6b3N0cmVhbSYgb3BlcmF0b3I8PCggc3RkOjpvc3RyZWFtJiBzdHJlYW0sIGNvbnN0IHRpbWVzdGFtcCYgdHMgKSB7CiAgICByZXR1cm4gc3RyZWFtIAogICAgICAgIDw8ICh0cy5ob3VyIDw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