Question: What am I doing wrong? C + + code below: A digital clock shows hours ( between 0 and 2 3 ) and minutes (

What am I doing wrong? C++ code below:
A digital clock shows hours (between 0 and 23) and minutes (between 0 and 59). Once a minute, it receives a pulse to advance the time. Complete the Clock class, using data members for the hours and minutes.
#include
using namespace std;
/**
A simulated digital clock.
*/
class Clock
{
public:
/**
Sets the clock to 00:00.
*/
void reset();
/**
Advances this clock to the next minute.
*/
void pulse();
/**
Gets the hours of this clock.
@return the hours (between 0 and 23)
*/
int get_hours() const;
/**
Gets the minutes of this clock.
@return the minutes (between 0 and 59)
*/
int get_minutes() const;
private:
int hours;
int minutes;
};
void Clock::reset()
{
hours=0;
minutes=0;
}
void Clock::pulse()
{
if (minutes<=58)
{
minutes= minutes+1;
}
else
{
hours=(minutes+1)/60;
minutes=(minutes+1)-(hours*60);
}
}
int Clock::get_hours() const
{
return hours;
}
int Clock::get_minutes() const
{
return minutes;
}
int main()
{
Clock my_clock; my_clock.reset();
for (int i =0; i <100; i++){ my_clock.pulse(); }
cout << my_clock.get_hours()<< endl;
cout << "Expected: 1"<< endl;
cout << my_clock.get_minutes()<< endl;
cout << "Expected: 40"<< endl;
for (int i =0; i <70; i++){ my_clock.pulse(); }
cout << my_clock.get_hours()<< endl;
cout << "Expected: 2"<< endl;
cout << my_clock.get_minutes()<< endl;
cout << "Expected: 50"<< endl;
for (int i =0; i <1270; i++){ my_clock.pulse(); }
cout << my_clock.get_hours()<< endl;
cout << "Expected: 0"<< endl;
cout << my_clock.get_minutes()<< endl;
cout << "Expected: 0"<< endl;
return 0;
}
My output is (the numbers are supposed to match the expected numbers):
1
Expected: 1
40
Expected: 40
1
Expected: 2
50
Expected: 50
1
Expected: 0
0
Expected: 0

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!