Answer to Question #214476 in C++ for Baneen Firas

Question #214476

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.


1
Expert's answer
2021-07-06T13:58:11-0400
#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;
}

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
New on Blog
APPROVED BY CLIENTS