Friday 11 August 2017

use the time structure from Exercise 9, and 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 format.



Use the time structure from Exercise 9, and 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 format.

SOLUTION:
#include<iostream.h>
#include<conio.h>
///////////////////////////////////////////////////////////// Define Structures ////////////////////////////////////////////////////////////
struct time
            {           int hours,minutes,seconds;     };
///////////////////////////////////////////////////// Define Structure Variables /////////////////////////////////////////////////////
time t[2],R_t;
void main()
{
long i,t_secs[2],t_sec;
char dummychar;
clrscr();
///////////////////////////////////////////////////////////////// Take Input ////////////////////////////////////////////////////////////////////
for(i=0;i<2;i++)
                        {
                        cout<<"Enter Hours, Minutes & Seconds: ";
                                    cin>>t[i].hours>>dummychar>>t[i].minutes>>dummychar>>t[i].seconds;
                        }
/////////////////////////////////////////////////////////// Calculate New Time //////////////////////////////////////////////////////////
for(i=0;i<2;i++)
                        t_secs[i]=(t[i].hours*3600)+(t[i].minutes*60)+t[i].seconds;
t_sec=t_secs[0]+t_secs[1];
R_t.hours=t_sec/3600;           t_sec=t_sec-(R_t.hours*3600);
R_t.minutes=t_sec/60;            t_sec=t_sec-(R_t.minutes*60);
R_t.seconds=t_sec;
////////////////////////////////////////////////////////////////////// Output /////////////////////////////////////////////////////////////////////
cout<<"\nNew Time is: "<<R_t.hours<<":"<<R_t.minutes<<":"<<R_t.seconds<<"\n";
getch();
}

OUTPUT:

0 comments: