Question: Please fix the following code and compile it! #include #include #include #include Time.h #include using namespace std; //////////////////////Conversion methods/////////////////////////////////////// long hrs_to_ms(int i) { if (i

Please fix the following code and compile it!

#include

#include

#include

#include "Time.h"

#include

using namespace std;

//////////////////////Conversion methods///////////////////////////////////////

long hrs_to_ms(int i)

{

if (i < 0)

cerr << "Method hrs_to_ms: non-positive value given. ";

return i*60*60*1000; //hrs-->min-->sec-->ms

}

long min_to_ms(int i)

{

if (i < 0)

cerr << "Method min_to_ms: non-positive value given. ";

return i*60*1000;

}

long sec_to_ms(int i)

{

if (i < 0)

cerr << "Method sec_to_ms: non-positive value given. ";

return i*1000;

}

int time_in_ms(long t)

{

return t % 1000;

}

int time_in_sec(long t)

{

return (t/1000)%60;

}

int time_in_min(long t)

{

return (t/1000/60)%60;

}

int time_in_24hrs(long t)

{

return (t/1000/60/60)%24;

}

int time_in_12hrs(long t)

{

return (t/1000/60/60)%12;

}

////////////////////////End Conversion Methods//////////////

//Default Constructor: Time is at 0 ms, not 24 hr format

Time::Time()

: t_ms{0}, is_24_hr{false}

{}

//Overloaded Constructor: milliseconds only

Time::Time(long time)

: t_ms{time}, is_24_hr{false}

{

if (time < 0)

cerr << "Time Constructor: non-positive value given for time.";

}

//Overloaded Constructor: Hours, minutes, seconds, ms format

Time::Time(int hours, int minutes, int seconds, int milli )

{

//Error checking: positive values

if (hours < 0 || minutes < 0 || seconds < 0 || milli < 0)

cerr <<"Time Constructor: non-positive value given ";

if (hours > 23)

cerr << "Time Constructor: invalid hour value ";

if (minutes >= 60)

cerr << "Time Constructor: invalid minute value ";

if (seconds > 60)

cerr << "Time Constructor: invalid seconds value ";

if (milli > 999)

cerr << "Time Constructor: invalid milliseconds value ";

long h_conv = hrs_to_ms(hours);

long m_conv = min_to_ms(minutes);

long s_conv = sec_to_ms(seconds);

t_ms = h_conv + m_conv + s_conv + milli;

is_24_hr = false;

}

//Destructor

Time::~Time(void)

{}

////////////////////////////////Methods////////////////////////////////////////////

long Time::asLong() const

{

return t_ms;

}

string Time::toString() const

{

int hrs, min, sec, ms;

ostringstream s;

/*Hour assignment: check for 24 hour flag, adjust hour assignment an AM/PM output */

if (is_24_hr)

{

hrs = time_in_24hrs(t_ms);

min = time_in_min(t_ms);

sec = time_in_sec(t_ms);

ms = time_in_ms(t_ms);

s << hrs << ":" << min << ":" << sec << ":" << ms;

}

/* Output as 12-hour time, add AM/PM to output string */

else

{

/* Check for midnight state */

if (time_in_12hrs(t_ms) == 0)

hrs = 12;

else

hrs = time_in_12hrs(t_ms);

min = time_in_min(t_ms);

sec = time_in_sec(t_ms);

ms = time_in_ms(t_ms);

/* AM/PM check */

/* Check the time using time_in_24hrs, not time_in_12hrs, which is always < 12 */

if (time_in_24hrs(t_ms) < 12)

s << hrs << ":" << min << ":" << sec << ":" << ms << " AM";

else

s << hrs << ":" << min << ":" << sec << ":" << ms << " PM";

}

return s.str();

}

void Time::set24Hour(bool value)

{

/* DEBUG */

//cout << "set24Hour: value is " << value;

is_24_hr = value;

}

bool Time::is24Hour() const

{

return is_24_hr;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!