Answer to Question #337491 in C++ for Nour ramadan

Question #337491

Create a structure called time. Its three members, all type



int, should be called hours, minutes, and seconds. Write a



program that obtains two time values from the user in



12:59:59 format, 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 forma

1
Expert's answer
2022-05-05T09:33:45-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 1" << endl;
	cout<<"Enter hour: ";
	cin>>time1.hours;
	cout<<"Enter minute: ";
	cin>>time1.minutes;
	cout<<"Enter second: ";
	cin>>time1.seconds;


	cout<<"Enter a time value 2" << endl;
	cout<<"Enter hour: ";
	cin>>time2.hours;
	cout<<"Enter minute: ";
	cin>>time2.minutes;
	cout<<"Enter second: ";
	cin>>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;
	
	cout<<"\nThe total number of seconds time 1: "<<totalSeconds1<<" seconds\n";
	cout<<"The total number of seconds time 2: "<<totalSeconds2<<" seconds\n";
	
	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";
	
	cout<<"\nAdd two times\n";
	cout<<"The sum of two times: "<<sumSeconds<<" seconds\n";
	
	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;
}

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