I’m trying to do C++ exercises (specifically the Clock exercise on the main track), and every time I run it, it says it times out. My code doesn’t have any loops in it so I thought that was weird, so I tried testing the next exercise without any code added (just the empty namespaces), and it timed out again. It should have given me a fail state instead of timing out, right? Is this a site-wide issue, or is there something I can do to fix it?
Here’s my clock code if you want to make sure I’m not being stupid:
//clock.h
#if !defined(CLOCK_H)
#define CLOCK_H
#include <string>
#include <stdint.h>
#include <stdexcept>
namespace date_independent {
typedef uint8_t CLK_U;
class clock {
private:
const static int CLK_MAX_HRS, CLK_MAX_MIN;
struct h_and_m {
CLK_U hour;
CLK_U minute;
h_and_m(void) : h_and_m(0, 0) {};
h_and_m( CLK_U init_hour, CLK_U init_minute ) : hour(init_hour), minute(init_minute) {};
} _time;
static h_and_m loop( int new_hour, int new_minute );
public:
clock( int init_hour, int init_minute );
static clock at( int init_hour, int init_minute );
clock& plus( int addend_minutes );
operator std::string(void);
bool operator==(const clock& comparator);
};
} // namespace date_independent
#endif // CLOCK_H
//clock.cpp
#include "clock.h"
namespace date_independent {
const int clock::CLK_MAX_HRS = 24;
const int clock::CLK_MAX_MIN = 60;
clock::h_and_m clock::loop( int new_hour, int new_minute ) {
//loops the minutes from 0 to 60, borrowing or adding from the hours as needed
while (new_minute >= CLK_MAX_MIN || new_minute < 0) {
if (new_minute >= CLK_MAX_MIN) {
new_minute -= CLK_MAX_MIN;
new_hour++;
} else if (new_minute <= 0) {
new_minute += CLK_MAX_MIN;
new_hour--;
} else {
throw std::logic_error("Something's wrong with the looper - minutes");
}
}
//loops the hours from 0 to 24
while (new_hour >= CLK_MAX_HRS || new_hour < 0) {
if (new_hour >= CLK_MAX_HRS)
new_hour -= CLK_MAX_HRS;
else if (new_hour < 0)
new_hour += CLK_MAX_HRS;
else
throw std::logic_error("Something's wrong with the looper - hours");
}
return clock::h_and_m( CLK_U(new_hour), CLK_U(new_minute) );
}
clock::clock( int init_hour, int init_minute ) {
_time = loop( init_hour, init_minute );
}
clock clock::at( int init_hour, int init_minute ) {
return clock( init_hour, init_minute );
}
clock& clock::plus( int a_minutes ) {
int sum( int(_time.minute) + a_minutes );
_time = loop( int(_time.hour), sum );
return *this;
}
clock::operator std::string(void) {
std::string output("");
if (_time.hour < 10) output += "0";
output += std::to_string( _time.hour );
output += ":";
if ( _time.minute < 10 ) output += "0";
output += std::to_string( _time.minute );
return output;
}
bool clock::operator==(const clock& comparator) {
return ( (_time.hour == comparator._time.hour) && (_time.minute == comparator._time.minute) );
}
} // namespace date_independent
My personal test code is below.
#include <iostream>
int main()
{
date_independent::clock test(23, 59);
test.plus(100000);
std::cout << std::string(test) << "\n";
if ( test == date_independent::clock::at(33, 99) ) std::cout << "Equal\n";
else std::cout << "Unequal\n";
return 0;
}
The output is
10:39
Equal