Use a structure called Time to write a C++ program that asks the user to enter a time value in hours, minutes, and seconds. The program should then print out the total number of seconds represented by this time value.
#include <iostream>
#include <ctime>
using namespace std;
struct Time
{
int hr;//Hour
int mn;//Minut
int sec;//Second
//The method input time
void InputTime()
{
cout << "Hours: ";
cin >> this->hr;
cout << "Minuts: ";
cin >> this->mn;
cout << "Seconds: ";
cin >> this->sec;
}
//Implement method convert time to seconds
long long toSeconds()
{
long long ans = 0;
ans = this->hr * 60 * 60 + this->mn * 60 + this->sec;//Convert
return ans;
}
};
int main()
{
Time tim;
tim.InputTime();
cout << "Seconds: " << tim.toSeconds() << endl;
return 0;
}
Comments
Leave a comment