I am attempting to make an alarm clock however I dont know how to make it advanc
ID: 3625319 • Letter: I
Question
I am attempting to make an alarm clock however I dont know how to make it advance in seconds or how to make it stop when the alarm time is reached. I need a overload operator and a loop and cannot figure out how to do either of these.#include <iostream>
using namespace std;
class Time
{
public:
Time();
Time(int hours2, int minutes2, int seconds2);
int getHours() const;
int getMinutes() const;
int getSeconds() const;
bool operator==(const Time &other) const
void setHours( int hours2);
void setMinutes( int minutes2);
void setSeconds( int seconds2);
void show() const;
void goClock();
private:
int hours, minutes, seconds, over;
};
Time::Time()
{
hours=minutes=seconds=over=0;
}
Time::Time(int hours2, int minutes2, int seconds2)
{
hours = hours2;
minutes = minutes2;
seconds = seconds2;
}
int Time::getHours() const
{
return hours;
}
int Time::getMinutes() const
{
return minutes;
}
int Time::getSeconds() const
{
return seconds;
}
void Time::setHours(int hours2)
{
hours = hours2;
}
void Time::setMinutes(int minutes2)
{
minutes = minutes2;
}
void Time::setSeconds(int seconds2)
{
seconds = seconds2;
}
void Time::goClock()
{
seconds+1;
if (seconds >60)
{
int temp =0;
temp = minutes+1;
setMinutes(temp);
}
if (minutes >60)
{
int temp =0;
temp = hours+1;
setHours(temp);
}
if (hours <24)
{
int temp = 0;
setHours(temp);
}
}
void Time::show() const
{
if (hours >12)
{
hours = hours-12
int over = 1;
}
cout<<hours<<":"<<minutes<<":"<<seconds<<" ";
if (over=1)
{ cout<<"AM"<<endl;
}
else
{ cout<<"PM"<<endl;
}
}
int main()
{
int hr,mn,sec,am;
cout<<"Please enter hour of alarm in 24 hour format";
cin>>hr;
cout<<"Please enter minutes of alarm";
cin>>mn;
sec=00;
Time t2=Time(hr,mn,sec,am);
Time t1=Time(12,00,00,0);
t1.show();
t2.show();
system("pause");
}
Explanation / Answer
I added some missing functions for you some other changes are highlighted in red. Notes in blue. I modified the constructor to accept 4 ints since that seems to be what you wanted to do. Functions inside a class don't have to use set or get functions but it worked fine so I just left it. I removed const from the show function because you were trying to change over. You can't modify variables inside a const function.
#include <iostream>
using namespace std;
class Time
{
public:
Time();
Time(int hours2, int minutes2, int seconds2, int over2);
int getOver () const;
int getHours() const;
int getMinutes() const;
int getSeconds() const;
bool operator==(const Time &other) const;
void setOver(int over2);
void setHours( int hours2);
void setMinutes( int minutes2);
void setSeconds( int seconds2);
void show();
void goClock();
private:
int hours, minutes, seconds, over;
};
Time::Time()
{
hours=minutes=seconds=over=0;
}
Time::Time(int hours2, int minutes2, int seconds2, int over2)
{
hours = hours2;
minutes = minutes2;
seconds = seconds2;
over = over2;
}
int Time::getOver() const
{
return over;
}
int Time::getHours() const
{
return hours;
}
int Time::getMinutes() const
{
return minutes;
}
int Time::getSeconds() const
{
return seconds;
}
void Time::setOver(int over2)
{
over = over2;
}
void Time::setHours(int hours2)
{
hours = hours2;
}
void Time::setMinutes(int minutes2)
{
minutes = minutes2;
}
void Time::setSeconds(int seconds2)
{
seconds = seconds2;
}
void Time::goClock()
{
seconds++; // Use either ++ to add one or +=1
if (seconds > 60)
{
seconds = 0;
int temp =0;
temp = (minutes + 1);
setMinutes(temp);
}
else;
if (minutes > 60)
{
minutes=0;
int temp =0;
temp = (hours + 1);
setHours(temp);
}
else;
if (hours > 24)
{
int temp = 0;
setHours(temp);
}
else;
if (hours > 12)
{
int temp = 1;
setOver(temp);
}
else
{
int temp = 0;
setOver(temp);
}
}
void Time::show()
{
if (hours > 12)
{
hours = 0;
}
cout<<hours<<":"<<minutes<<":"<<seconds<<" ";
if (over == 0) // It is important to use two equals == in a comparison
{
cout<<"AM"<<endl;
}
else
{
cout<<"PM"<<endl;
}
}
int main()
{
int hr = 0; // I went ahead and set initial values for these variables it is just a good practice.
int mn = 0;
int sec = 0;
int am = 0;
cout<<"Please enter hour of alarm in 24 hour format: ";
cin>>hr;
cout<<"Please enter minutes of alarm: ";
cin>>mn;
sec=00;
Time t2=Time(hr,mn,sec,am);
Time t1=Time(12,00,00,0);
t1.show();
t2.show();
do {
t1.goClock ();
t1.show();
} while (!((t1.getHours () == t2.getHours ()) && (t1.getMinutes () == t2.getMinutes ()) && (t1.getSeconds () == t2.getSeconds ()) && (t1.getOver () == t2.getOver ())));
// if you dont want to show the time every second remove t1.show from the loop
system("pause");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.