Displaying addition of times using structure and function

  c++, function, structure

I have taken input values for two different Times from the user and have to display the addition of them. I have created a structure and two functions of the structure for Time 1 and Time 2. But, whenever I enter minutes or seconds greater than 70 or 80, the format of the resultant time (Addition of Time 1 and Time 2) does not display in the correct format.

Code:

#include <iostream>

using namespace std;
struct time{
int hour;
int minute;
int seconds;
};
time Time1(time);
time Time1(time a)
{
           if(a.minute >=60 && a.seconds>=60)
   {
       a.minute-=60;
       a.hour++;
       a.seconds-=60;
       a.minute++;
   }

    cout<<"Time 1 is: "<<a.hour<<":"<<a.minute<<":"<<a.seconds<<endl;

}
time Time2(time);
time Time2(time b)
{

       if(b.minute >=60 && b.seconds>=60)
   {
       b.minute-=60;
       b.hour++;
       b.seconds-=60;
       b.minute++;
   }

    cout<<"Time 2 is: "<<b.hour<<":"<<b.minute<<":"<<b.seconds<<endl;
}
int main()
{
    time t1;
    time t2;
    time t3;
    cout<<"Enter hours: ";
    cin>>t1.hour;
    cout<<"Enter minutes: ";
    cin>>t1.minute;
    cout<<"Enter seconds: ";
    cin>>t1.seconds;
    Time1(t1);
    cout<<"Enter hours: ";
    cin>>t2.hour;
    cout<<"Enter minutes: ";
    cin>>t2.minute;
    cout<<"Enter seconds: ";
    cin>>t2.seconds;
    Time2(t2);
    cout<<"Adding Time 1 and Time 2 = ";
   t3.hour=t1.hour+t2.hour;
    t3.minute=t1.minute+t2.minute;
    t3.seconds=t1.seconds+t2.seconds;
      if(t3.minute >=60)
   {
       t3.minute-=60;
       t3.hour++;
   }  else if(t3.seconds>=60)
   {
       t3.seconds-=60;
       t3.minute++;
   }
    cout<<t3.hour<<":"<<t3.minute<<":"<<t3.seconds<<endl;

    return 0;
}

Source: Windows Questions C++

LEAVE A COMMENT