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. This can be in 12:59:59 format, or
each number can be entered at a separate prompt (“Enter hours:”, and so forth).
The program should then store the time in a variable of type struct time, and
finally print out the total number of seconds represented by this time value:
long
totalsecs = t1.hours*3600 + t1.minutes*60 + t1.seconds
SOLUTION:
#include<iostream.h>
#include<conio.h>
/////////////////////////////////////////////////////////////
Define Structures
////////////////////////////////////////////////////////////
struct time
{
int hours,minutes,seconds;
};
///////////////////////////////////////////////////// Define Structure Variables
/////////////////////////////////////////////////////
time t1;
void main()
{
long totalsecs;
char dummychar;
clrscr();
/////////////////////////////////////////////////////////////////
Take Input
////////////////////////////////////////////////////////////////////
cout<<"Enter Hours, Minutes
& Seconds: ";
cin>>t1.hours>>dummychar>>t1.minutes>>dummychar>>t1.seconds;
////////////////////////////////////////////////////////////
Calculate Seconds
///////////////////////////////////////////////////////////
totalsecs=(t1.hours*3600)+(t1.minutes*60)+t1.seconds;
//////////////////////////////////////////////////////////////////////
Output
/////////////////////////////////////////////////////////////////////
cout<<"\nTotal Seconds are:
"<<totalsecs<<"\n";
getch();
}
OUTPUT:
![](file:///C:\Users\MUHAMM~1\AppData\Local\Temp\msohtmlclip1\01\clip_image002.jpg)
0 comments: