Write a program having a structure named Time which has three integer data items i.e. hour, minute and second. The task is to add the variables of the Time data type though a function

void AddTime(Time *time1, Time *time2)

which takes as arguments the addresses of two Time type variables, adds these variables and stores the result in time2. The function must satisfy the following:

  1. If second exceeds 60 then add 1 in minutes and subtract 60 from seconds.
  2. If minute exceeds 60 then add 1 in hours and subtract 60 from minutes.
  3. If hour exceeds 24 then subtract 24 from hours.

Test this function and print the result in the calling function.

[cpp]
// Acknowledgment: Wahib-ul-Haq
#include
#include

using namespace std;

struct Time
{
int hour, minute, second;
};

void AddTime(Time *, Time * );

void main()
{
Time var1, var2;
for(int i=0;i<1;i++) { cout<<"for structure variable #"<>var1.hour;
cout<<"for structure variable #"<>var1.minute;
cout<<"for structure variable #"<>var1.second;
cout<>var2.hour;
cout<<"for structure variable #"<>var2.minute;
cout<<"for structure variable #"<>var2.second;

}

AddTime(&var1, &var2);

cout<<"\nNew hours = "<

Shares 11
11 Shares
Share8
+13
Tweet
Share
Stumble