Time Format(Military to Standard)
Nov 30, 2014 at 10:22pm UTC
I have this program in c++, It will convert from military format time to standard time format, but is not working, if you type 1830 it will show 6:30AM, and have to be 6:30pm, and if you type 0030 it will show 0:30AM and have to be 12:30AM, If somebody know what I'm doing wrong please let me know thanks...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
#include<iostream>
#include<iomanip>
using namespace std;
class Time
{
protected :
int hour;
int min;
int sec;
public :
Time()
{hour = 0; min = 0; sec = 0;}
Time(int h, int m, int s)
{hour = h; min = m; sec = s;}
int gethour() const
{return hour;}
int getmin() const
{return min;}
int getsec() const
{return sec;}
};
class MilTime : public Time
{
private : int milhours;
int milsec;
public :
MilTime(int hours, int seconds)
{
milhours = hours;
milsec = seconds;
}
void setTime(int mhours, int mseconds);
int gethour();
int getstandarhour();
};
void MilTime::setTime(int mhours, int mseconds)
{
milhours = mhours;
milsec = mseconds;
hour = (milhours / 100);
min = milhours % 100;
sec = milsec;
}
int MilTime::gethour()
{
return milhours;
}
int MilTime::getstandarhour()
{
return hour % 12;
}
int main()
{
int hr,sec;
MilTime Time1(0, 0);
cout << "Enter Military Format Time: " ;
cin >> hr;
sec = hr % 10;
Time1.setTime(hr, sec);
cout << "Military Format: " << Time1.gethour() << "Hours" << endl;
if (Time1.gethour() / 12 == 1)
{
cout << "Standar Format: " << Time1.getstandarhour() << ":" ;
if (Time1.getmin() == 0)
{
cout << Time1.getmin() << "0" << "PM" << endl;
}
else
cout << Time1.getmin() << "PM" << endl;
}
else
{
cout << "Standard Format: " << Time1.getstandarhour() << ":" ;
if (Time1.getmin() == 0)
{
cout << Time1.getmin() << "0" << "AM" << endl;
}
else
cout << Time1.getmin() << "AM" << endl;
}
system("pause" );
return 0;
}
Topic archived. No new replies allowed.