Answer to Question #77001 in C++ for Talha Saeed

Question #77001
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.
1
Expert's answer
2018-05-09T07:23:35-0400
#include <iostream>
#include <stdio.h>

using namespace std;

//---------------------------------------------------------------------------
// Write a function called hms_to_sec() that takes three int values &ndash;
// 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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS