Making a tick function for a Clock

I have code that makes a Clock in HH:MM:SS AM/PM format.It then adds 2 hours, 30 minutes, and 15 seconds. THEN it ticks forward 1 second each time the tick() function is called. I'm pretty sure the tick() function isn't supposed to have any arguments. I have in the code two different things I have tried(one commented out) but it still won't do anything when I print out the time again.It just prints the same time that it would've printed had I not used the tick().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
  #include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Clock
{
private:
	int mHours; //(range 0-23)
	int mMinutes; //(range 0-59)
	int mSeconds; //(range 0-59)
public:
	Clock();
	Clock(int);
	Clock(int, int);
	Clock(int, int, int);
	int getHours()  
		{return mHours;}
	int getMinutes()  
		{return mMinutes;}
	int getSeconds()  
		{return mSeconds;}
	void setHours(int hourValue) {
		if ( hourValue >= 0 && hourValue <23) { 
			mHours = hourValue;
		} else if(hourValue<0) { 
			mHours = 0;
		} else if(hourValue>12) {
			mHours = 12;
		}
	}
	void setMinutes(int minuteValue) {
		if ( minuteValue >= 0 && minuteValue < 60) {
			mMinutes = minuteValue;
		} else {
			mMinutes = 0;
		}	
	}
	void setSeconds(int secondValue) {
		if ( secondValue >= 0 && secondValue < 60) {
			mSeconds = secondValue;
		} else {
			mSeconds = 0;
		}
	}
	bool isMorning();
	bool isAfternoon();
	bool isEvening();
	void tick();
	friend ostream &operator<<(ostream &, Clock&);	
};
	ostream &operator<<(ostream &out, Clock &time){
		string display;	
		if(time.mHours >12)
		{	time.mHours = time.mHours - 12;
			display = "PM";
		}else{
			display = "AM";
		}

		
		out << setfill('0') << setw(2)<<time.mHours;
		out << ":"<<setfill('0')<<setw(2)<< time.mMinutes;
		out << ":"<<setfill('0')<<setw(2)<< time.mSeconds
		<< " "<<display << endl; 
		return out;
}
	Clock::Clock() {
		setHours(12);
		setMinutes(0);
		setSeconds(0);
		}
	Clock::Clock(int hourValue) 
		{setHours(hourValue);}
	Clock::Clock(int hourValue, int minuteValue) 
		{setHours(hourValue);
		setMinutes(minuteValue);}
	Clock::Clock(int hourValue, int minuteValue, int secondValue) 
		{setHours(hourValue);
		setMinutes(minuteValue);
		setSeconds(secondValue);}
	

	bool Clock::isMorning() {
		if ( mHours < 12) {
			return true;
		}else{
			return false;
		}
	}
	bool Clock::isAfternoon() {
		if ( mHours >= 12 && mHours < 18) {
			return true;
		}else{ 
			return false;
		}
	}
	bool Clock::isEvening() {
		if ( mHours < 18) {
			return false;
		}else{
			return true;
		}
	}
	void Clock::tick() {
		setSeconds((getSeconds() + 1 ) % 60);
	
		if ( getSeconds()==0)
		{
			setMinutes((getMinutes() + 1) % 60);
			if (getMinutes() == 0)
			{
				setHours((getHours() % 12) +1);
			}
		}
		}

		/*if (mSeconds < 60)
			mSeconds = mSeconds + 1;
		else 
			mSeconds = 0;
		if (mMinutes < 60 && mSeconds == 0)
			mMinutes = mMinutes + 1;
		else 
			mMinutes = 0;
		if (mHours < 24 && mMinutes == 0 && mSeconds == 0)
			mHours = mHours + 1;
		else 
			mHours = 0;*/
		

void printTimeMessage(Clock a) {
		
		cout << "The current time is "<< a << endl;
}

int main() {
	
	int hour;
	int minute;
	int second;
	Clock a(hour,minute,second);

	cout << "What is the hour?" << endl;
	cin >> hour;
	
	cout << "What is the minute?" << endl;
	cin >> minute;

	cout << "What is the second?" << endl;
	cin >> second;

printTimeMessage(Clock(hour,minute,second));

	hour= hour +2;
	minute = minute + 30;
	second = second + 15;
	cout << "Back to the Future!"; 
	printTimeMessage(Clock(hour,minute,second));

	a.tick();
	printTimeMessage(Clock(hour,minute,second));	

return 0;
}
	
Can anyone help with this tick function? I have no clue what I should be doing.
Well, the tick() function shouldn't be using the "get" functions. It is a member of the class- it has access to private variables. It also shouldn't be using set functions- again, access to the variables themselves. If you think about it like that, simply add one to seconds. If seconds is equal to 60, set it to zero and add one to minutes. If minutes is equal to 60, set it to zero and add 1 to hours. If hours is equal to 12, set it equal to 1 and flip the AM/PM state.
Well, in the commented out section of the code I had used member variables to the class and it hadn't worked. It made me wonder if I'm calling it wrong or something because when I use the commented out section instead, it still has the same time when I print it out.
Well, I think the issue here is that with Clock a, you construct it before there are values inputted for hours, minutes, and seconds. You're putting gibberish in for those values, hence the non-functionality. Construct the clock after you have values in for those three. As for the printTimeMessage function, simply make it take an object of type clock. What you're doing is... well, I don't really know what you're passing to it. Just replace Clock(hour,minute,second) with [/code]Clock a[/code] for what is being passed to it. That way, in main(), you can just pass object a to the function, then use the get functions for the clock in the printTimeMessage function for making any manipulations.
I'm not really sure what you mean by construct the clock after I have values in for the three variables. Do you mean I need to put the Clock class below the main? I'm sorry I'm new to this.
Last edited on
No, no. You'd put line 143 at line 153.
Oh okay I see. It actually ticks now! I need to work out a few things but it works nonetheless. Thank you. I don't see why adding 2 hours 30 minutes and 15 seconds no longer works though. Instead of it adding the time, it just displays the same time. Why is this?
Replace line for adding to the time (hours = hours + 2) with sethour/setminute/setsecond. So, for line 156 of your original code, it would be setHours(hour+2);. Repeat for those three, and it will work as expected.
"setHours was not declared in the scope"
Wait I changed it to a.setHours(hours+2) and it worked fine. Thank you for your help
Topic archived. No new replies allowed.