0


Hey Friends Today I Will Tell You How To Convert Time From 12 To 24 Hours By Using Operator Overloading. From Civilian Time To Military Time. In C++ (OOP)

First Of All You Should Have To know About Both These Times.

 TABLE 

__________________________________________

12-Hour Time               24-Hour Time __________________________________________

12:00 a.m. (midnight)           00:00
12:01 a.m.                      00:01
1:00 a.m.                       01:00
6:00 a.m.                       06:00
 11:59 a.m                       11:59
12:00 p.m. (noon)              12:00
12:01 p.m.                     12:01
6:00 p.m.                      18:00
11:59 p.m.                     23:59




Note that 12 a.m. (midnight) in civilian time is 00 hours in military time. There is no 0 hour in civilian time.
Use This Code For Converting Time If You Do not Understand Then Feel Free To Comment I will Reply You As Soon As It Is Possible.
Given Below Code Is The Most Simplest Code....
 ////////////////////////////////////////////////////////////////////////////////////////
#include<iostream>
#include<string.h>
using namespace std;
class time24
{
    private:
        int hrs,min,sec;
    public:
    time24(int hrr,int minn,int seco):hrs(hrr),min(minn),sec(seco){}
    void showdata()
    {
        cout<<"Time in 24 hrs is = ";
        if(hrs<10) cout<<'0';
        cout<<hrs<<':';
        if(min<10) cout<<'0';  
        cout<<min<<':';
        if(sec<10) cout<<'0';
        cout<<sec;
    }   
};
class time12
{
private:
int hours;
int minutes;
string am_pm;
public:
    time12():hours(0),minutes(0),am_pm(""){}
    void getdata()
    {
    hrs:   
    cout<<"\nPlease Enter 12 hrs Time\nEnter Hours (1 to 12) ";   
    cin>>hours;
    if(hours>12)
    {
    cout<<"\nSorry The Hours You Enter Are Invalid\n\n\t===>Input Hours Again<===\n\n\n";
    goto hrs;
    }
    min:
    cout<<"\nEnter Minutes ";
    cin>>minutes;
    if (minutes>59)
    {
        cout<<"Sorry The Minutes You Enter Are Invalid\n\n\t===>Input Minutes Again<===\n\n\n";
        goto min;
    }
    ampm:   
    cout<<"Please Enter AM or PM According To Your Time";
    cin>>am_pm;
    if(am_pm!="am"&&am_pm!="pm")
    {
    cout<<"\nYou Enter "<<am_pm<<"  It is neither AM nor PM\n\nPlease Enter Again\n\n";
    goto ampm;
    }
    }
operator time24()
{
    int sec=0;
    if(am_pm=="am")
     {
    if(hours==12)
            {
                    hours=0;
                   
            }
     }   
    else if(am_pm=="pm")
    {
    if(hours==12){}
   
    else
    {
    hours+=12;   
    }
   
    if(hours==24)
    {
        hours=0;       
    }
    }   
    return time24(hours,minutes,sec);
}
};
int main (void)
{
    while(true){
    cout<<"\n\n";
time12 t12;
t12.getdata();
time24 t24 = (time24)t12;
t24.showdata();       
}
return 0;
}
 

Post a Comment

 
Top