Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please fix the following code and compile it! #include <iostream> #include <type

ID: 3669991 • Letter: P

Question

Please fix the following code and compile it!

#include <iostream>

#include <typeinfo>

#include <string>

#include "Time.h"

#include <sstream>

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;

}

Explanation / Answer

I did't see any Error in the above program.

the problem with the program may be in Time.h header file.

Note: Check the functions declared in header file.
import necessary functions in the header file.

*******

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;

}

*****