#include <iostream>
#include <stdio.h>
using namespace std;
//---------------------------------------------------------------------------
// Write a function called hms_to_sec() that takes three int values –
// for hours, minutes, and seconds as arguments, and return the equivalents time
// in seconds (type long).
// Create a program that exercises this function by repeatedly obtaining
// a time value in hours, minutes and seconds from the user (format 12:59:59),
// calling the function, and displaying the value of the seconds it returns.
//---------------------------------------------------------------------------
long hms_to_sec(int hours, int minutes, int seconds) {
return (hours*60 + minutes) * 60 + seconds;
}
main() {
cout << "Input time value in hours, minutes and seconds (format 12:59:59)" << endl;
cout << ">> ";
char s[20];
cin >> s;
cout << endl;
int hours, minutes, seconds;
sscanf(s,"%2d:%2d:%2d", &hours, &minutes, &seconds);
cout << "Time is " << hours << ":" << minutes << ":" << seconds << endl;
cout << "Number of the seconds is " << hms_to_sec(hours, minutes, seconds) << endl;
cout << "Press any key...";
cin.clear(); cin.ignore(); cin.get();
return 0;
}
Comments
Leave a comment