Problem
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
Code
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct time {
int hours;
int minutes;
int seconds;
};
//function prototype
void add (int,int,int,int,int,int);
int main()
{
struct time t1,t2;
//1st time
printf ("Enter 1st time.");
printf ("\n\nEnter Hours: ");
scanf ("%d",&t1.hours);
printf ("\nEnter Minutes: ");
scanf ("%d",&t1.minutes);
printf ("\nEnter Seconds: ");
scanf ("%d",&t1.seconds);
printf ("\n\nThe Time is %d:%d:%d",t1.hours,t1.minutes,t1.seconds);
//Second time
printf ("\n\n\nNow, please type the 2nd time.");
printf ("\n\nEnter Hours: ");
scanf ("%d",&t2.hours);
printf ("\nEnter Minutes: ");
scanf ("%d",&t2.minutes);
printf ("\nEnter Seconds: ");
scanf ("%d",&t2.seconds);
printf ("\n\nThe Time is %d:%d:%d",t2.hours,t2.minutes,t2.seconds);
//function call
add (t1.hours,t1.minutes,t1.seconds,t2.hours,t2.minutes,t2.seconds);
printf ("\n\nPress any key to exit.");
getch();
return 0;
}
//function definition
void add (int x,int y,int z,int a,int b,int c)
{
int h,m,s;
h = x + a;
m = y + b;
s = z + c;
printf ("\n\n\nSum of the two time's is %d:%d:%d",h,m,s);
}
Code
|
Time Sum Using Structure in C/C++ Language |