Answer to Question #186032 in C++ for python

Question #186032

(Create a structure called time. Its three members, all type int, should be called

hours, minutes, and seconds. Write a program that prompts the user to enter a

time value in hours, minutes, and seconds. The program should then store the time

in a variable of type struct time, and finally print out the total number of seconds.

Use the time structure from above and write a program that obtains two

time values from the user, stores them in struct time variables, converts each one)


to seconds (type int), adds these quantities, converts the result back to hours-

minutes- seconds, stores the result in a time structure, and finally displays the


result in 12:59:59 format


1
Expert's answer
2021-04-27T00:02:51-0400
#include <iostream>
#include <iomanip>
using namespace std;


struct Time{
	 int hours;
	 int minutes;
	 int seconds; 
};


int main(){  
	struct Time time1;
	struct Time time2;
	struct Time timeSum;
	cout<<"Enter a time value in hours, minutes, and seconds 1: ";
	cin>>time1.hours>>time1.minutes>>time1.seconds;
	//write a program that obtains two time values from the user, 
	//stores them in struct time variables, converts each one) to seconds (type int)
	cout<<"Enter a time value in hours, minutes, and seconds 2: ";
	cin>>time2.hours>>time2.minutes>>time2.seconds;
	int totalSeconds1=time1.hours*60*60+time1.minutes*60+time1.seconds;
	int totalSeconds2=time2.hours*60*60+time2.minutes*60+time2.seconds;
	int sumSeconds=totalSeconds1+totalSeconds2;
	//print out the total number of seconds.
	cout<<"\nThe total number of seconds time 1: "<<totalSeconds1<<" seconds\n";
	cout<<"The total number of seconds time 2: "<<totalSeconds2<<" seconds\n";
	//stores the result in a time structure, and finally displays the result in 12:59:59 format
	cout<<"\nTime 1: "<<setw(2) << setfill('0')<<time1.hours<<":"<<setw(2) << setfill('0')<<time1.minutes<<":"<<setw(2) << setfill('0')<<time1.seconds<<"\n";
	cout<<"Time 2: "<<setw(2) << setfill('0')<<time2.hours<<":"<<setw(2) << setfill('0')<<time2.minutes<<":"<<setw(2) << setfill('0')<<time2.seconds<<"\n";
	//adds these quantities
	cout<<"\nAdd two times\n";
	cout<<"The sum of two times: "<<sumSeconds<<" seconds\n";
	//converts the result back to hours-minutes-seconds
	timeSum.minutes = sumSeconds / 60;
	timeSum.seconds = sumSeconds % 60;
	timeSum.hours = timeSum.minutes  / 60;
	timeSum.minutes  = timeSum.minutes  % 60;
	cout<<"Time sum: "<<setw(2) << setfill('0')<<timeSum.hours<<":"<<setw(2) << setfill('0')<<timeSum.minutes<<":"<<setw(2) << setfill('0')<<timeSum.seconds<<"\n";
	system("pause");
	return 0;
}


Output:


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