-1

plz write this program for me
write a program that input 2 time from the user and display the sum of this time in this format HH.MM.SS

2
Contributors
1
Reply
19
Views
3 Years
Discussion Span
Last Post by Deepak_17
-3
#include<iostream>
#include<iomanip.h>
using namespace std;
void convert(int,int&,int&,int&);
class Time


    #include<iostream>
    #include<iomanip.h>
    using namespace std;
    void convert(int,int&,int&,int&);
    class Time
    {
    public:
    int hour,minute,second;
    Time()
    {hour=0;
    minute=0;
    second=0;
    }
    ~Time()
    {hour=0;
    minute=0;
    second=0;
    }
    Time(int h,int m,int s)
    {hour=h;
    minute=m;
    second=s;
    }
    void get()
    {cout<<"Enter the time\n";
    cout<<"Hour: ";
    cin>>hour;
    cout<<"Minutes: ";
    cin>>minute;
    cout<<"Seconds: ";
    cin>>second;
    }
    void show()
    {cout<<setw(2)<<setfill('0')<<hour<<":"<<setw(2)<<setfill('0')<<minute<<":"<<setw(2)<<setfill('0')<<second<<endl;
    }
    Time operator + ( Time t)
    {int h,m,s;
    int totalsec;
    totalsec=(hour*3600+minute*60+second)+(t.hour*3600+t.minute*60+t.second);
    convert(totalsec,h,m,s);
    return Time(h,m,s);
    }
    Time operator - ( Time t)
    {int h,m,s,totalsec;
    totalsec=(hour*3600+minute*60+second)-(t.hour*3600+t.minute*60+t.second);
    if(totalsec<0)
    totalsec=-totalsec;
    convert(totalsec,h,m,s);
    return Time(h,m,s);
    }
    void convert(int totalsec,int& h,int& m,int& s)
    { s=totalsec%60;
    totalsec=totalsec/60;
    m=totalsec%60;
    h=(totalsec/60)%12;
    if(h==0)
    h=12;
    return;
    }
    };

    int main()
    { Time time1,time2,added,subtracted;
    cout.setf(ios::fixed,ios::floatfield);
    cout.precision(2);
    time1.get();
    time2.get();
    added=time1+time2;
    subtracted=time1-time2;
    cout<<"Time 1 : ";
    time1.show();
    cout<<"Time 2 : ";
    time2.show();
    cout<<"\nAdded :\n ";
    time1.show();
    cout<<"+";
    time2.show();
    cout<<"----------\n ";
    added.show();
    cout<<"\nSubtracted:\n ";
    time1.show();
    cout<<"-";
    time2.show();
    cout<<"----------\n ";
    subtracted.show();
    system("pause");
    }

Edited by pyTony: Code put in code block, do not give ready solution without effort next time, however!

Votes + Comments
Why you do this? >:(
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.