Alarm Clock Issues

closed account (4Eh5oG1T)
So I'm trying to program an alarm clock, and I feel i have most of the program there but whenever i go to launch, it says that there are errors in the exe and some obj file. My guess is that something in my code somewhere is goofy, but I have been at it for so long today I can't figure out what! Any help or even tips on the program itself is appreciated!

My Alarm.h file

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
#pragma once
#ifndef ALARMCLOCK_H
#define ALARMCLOCK_H

class alarmClock {
	public:
		alarmClock();
		alarmClock(int, int, int, char);

		void setTime(int, int, int, char);
		void setHour(int);
		void setMin(int);
		void setSecond(int);
		void setAM(char);

		alarmClock& operator++ ();

		int getHour();
		int getMinute();
		int getSecond();
		bool getAM();

		void displayTime();
		void setAlarm();
		void displayAlarm();
		void wakeUp();

	 private:
		int hour, minute, second;
		int alHour, alMinute, alSecond;
		char am;
		bool alarmOff;
};
#endif; 



My Main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include "Alarm.h"
#include "Alarm.cpp"
using namespace std;

int main() {
	int counter = 0;
	alarmClock Time;
	alarmClock Alarm;
	Time.displayTime();
	for (int i = 0; i < counter; i++) {
		//Seconds go up here second++
		Alarm.setSecond(counter);
	}
	Alarm.setAlarm();
	Alarm.displayAlarm();
	Alarm.wakeUp();
}



And the Alarm.cpp

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
#include "Alarm.h"
#include <iostream>
#include <iomanip>
using namespace std;
#include <stdexcept>


alarmClock::alarmClock() {
	setTime(hour = 12, minute = 0, second = 0, am = 'AM');
}
alarmClock::alarmClock(int hour, int minute, int second, char am) {
	setTime(hour, minute, second, am);
}

void alarmClock::setTime(int hour, int minute, int second, char am) {
	setHour(hour);
	setMin(minute);
	setSecond(second);
	setAM(am);
}

void alarmClock::setHour(int hr) {
	if (hr >= 1 && hr <= 12)
		hour = hr;
	else
		throw invalid_argument("Hour must be 1-12");
}

void alarmClock::setMin(int min) {
	if (min >= 0 && min < 60)
		minute = min;
	else
		throw invalid_argument("Minute must be 0-59");
}

void alarmClock::setSecond(int sec) {
	if (sec >= 0 && sec < 60)
		second = sec;
	else
		throw invalid_argument("Second must be 0-59");
}

void alarmClock::setAM(char am) {
	if (am == 'AM') {
		am = 'AM';
	} else {
		am = 'PM';
	}
}

alarmClock& alarmClock::operator++() {
	if (second != 59) {
		++second;
	}
	else {
		second = 0;
		if (minute != 59) {
			++minute;
		}
		else {
			minute = 0;
			setHour(hour + 1);
		}
		return *this;
	}
}

	int alarmClock::getHour() {
		return hour;
	}

	int alarmClock::getMinute() {
		return minute;
	}

	int alarmClock::getSecond() {
		return second;
	}

	bool alarmClock::getAM() {
		return am;
	}

	void alarmClock::displayTime() {
		cout << setfill('0') << setw(2) << hour << ':' << setfill('0') << setw(2)
			<< minute << ':' << setfill('0') << setw(2) << second << ' ' << am << endl;
	}

	void alarmClock::setAlarm() {
		char dot = ':';
			cout << "Please, enter the alarm time in the form 12:00:00 AM:";
			cin >> alHour;
		cin.get(dot);
		cin >> alMinute;
		cin.get(dot);
		cin >> alSecond;
		getAM();
		bool alarmOff = false;
	}

	void alarmClock::displayAlarm() {
		cout << "Alarm time was set on " << setfill('0') << setw(2) << alHour << ':' << setfill('0') << setw(2)
			<< alMinute << ':' << setfill('0') << setw(2) << alSecond << ' ' << am << endl;
	}

	void alarmClock::wakeUp() {
		if (hour == alHour && minute == alMinute && second == alSecond)
			cout << "WAKE UP!!!" << endl;
			//TODO Code for sound here
	}
Last edited on
closed account (48T7M4Gy)
.
Last edited on
You should post the the errors the compiler shows you.
The compiler helps you to understand the errors.
However:
- 'AM' and 'PM' are invalid, you should use "AM" and "PM" and std::string or an enum for the variable am.
- Alarm.cpp, line 9, did you intend using named arguments? You cannot use them, so it should be setTime(12, 0, 0, "AM");.
- Alarm.cpp, line 64, move return *this; at the end of the function.
- Alarm.cpp, line 80, the function getAM should return the same type of am.
- Main.cpp, line 3, you shouldn't #include a cpp, so delete that line.

The above are just to compile.
But, it doesn't work, so you should correct something else.
Example: function alarmClock::setAlarm() is wrong: see lines 97 and 98, what you wanted to do?
Function alarmClock::setAM is wrong: am is the parameter, this->am is the member variable.
Finally, the debugger helps you.
closed account (48T7M4Gy)
.
Last edited on
main.cpp
-----------
line 3: Never include a .cpp file. If alarm.cpp is included in your project, it will get compiled by itself. Including alarm.cpp will result in linker errors reporting multiply defined symbols.

alarm.cpp
------------
line 3,45,47: Your literal is going to get truncated to a single char since am is a single char. Your compiler should have warned your of this.

line 44-47: It's a bad practice to name your arguments the same as your class variables. The argument name (am) hides the class variable (am). The argument name takes precedence. You're setting the argument, not the class variable.

line 64: The return is misplaced. There are paths through the if that don't return a value.
The return should be after line 65.

Line 81: You're trying to return a char as a bool. if am is 'A' or 'P', this will always return true. You want:
 
  return (am == 'A');


alarm.h
-------
Lines 1-3: Using include guards and #pagma once is somewhat redundant.
While not all compilers support #pragma once, it's widely enough recognized that I wouldn't use both.

After taking out the #include of alarm.cpp file, I had no problem compiling and running your program.
Last edited on
closed account (4Eh5oG1T)
Thanks for the help everyone! I'll give all of these a chance one i get a good 5 minutes to sit down and try it all, thank you to everyone for being so helpful! =)
closed account (4Eh5oG1T)
Okay i got it to finally compile! I still have some logic errors that ill fix up, but once the program is finished i'll post my results here for anyone who wants to use it! =)
> I still have some logic errors that I will fix up, but once the program is finished I'll post my results here for anyone who wants to use it! =)

Okay. Good thing.
closed account (4Eh5oG1T)
Okay back again and stumped as to what I'm doing wrong. I have no more compilation errors, but whenever i run the program it seems to die when it finally gets to my wakeUp function...my updated code is below, and my input times for alarm times are going through so I am kind of stumped as to what to check

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
#include "Alarm.h"
#include <iostream>
#include <iomanip>
using namespace std;
#include <stdexcept>
#include <windows.h>

alarmClock::alarmClock() {
	setTime(12, 0, 0,'A');
}
alarmClock::alarmClock(int hour, int minute, int second, char am) {
	setTime(hour, minute, second, am);
}

void alarmClock::setTime(int hour, int minute, int second, char am) {
	setHour(hour);
	setMin(minute);
	setSecond(second);
	setAM(am);
}

void alarmClock::setHour(int hr) {
	if (hr >= 1 && hr <= 12)
		hour = hr;
	else
		throw invalid_argument("Hour must be 1-12");
}

void alarmClock::setMin(int min) {
	if (min >= 0 && min < 60)
		minute = min;
	else
		throw invalid_argument("Minute must be 0-59");
}

void alarmClock::setSecond(int sec) {
	if (sec >= 0 && sec < 60)
		second = sec;
	else
		throw invalid_argument("Second must be 0-59");
}


void alarmClock::setAM(char) {
	if (am == 'A') {
		am = 'A';
	} else {
		am = 'P';
	}
}

alarmClock& alarmClock::operator++() {
	if (second != 59) {
		++second;
	} else {
		second = 0;
		if (minute != 59) {
			++minute;
		} else {
			minute = 0;
			setHour(hour + 1);
		}
	}
	return *this;
}

	int alarmClock::getHour() {
		return hour;
	}

	int alarmClock::getMinute() {
		return minute;
	}

	int alarmClock::getSecond() {
		return second;
	}

	bool alarmClock::getAM() {
		return (am == 'A');
	}

	void alarmClock::displayTime() {
		cout << setfill('0') << setw(2) << hour << ':' << setfill('0') << setw(2)
		<< minute << ':' << setfill('0') << setw(2) << second << ' ' << am << endl;
	}

	void alarmClock::setAlarm() {
		char dot = ':';
		cout << "Please, enter the alarm time in the form 12:00:00 AM:\n";
		cout << "Hour:";
		cin >> alHour;
		cin.get(dot);
		cout << "Minute:";
		cin >> alMinute;
		cin.get(dot);
		cout << "Second:";
		cin >> alSecond;
		getAM();
		bool alarmOff = false;
	}

	void alarmClock::displayAlarm() {
		cout << "Alarm time was set on " << setfill('0') << setw(2) << alHour << ':' << setfill('0') << setw(2)
		<< alMinute << ':' << setfill('0') << setw(2) << alSecond << ' ' << am << endl;
		wakeUp();
	}

	void alarmClock::wakeUp() {
		if (hour == alHour && minute == alMinute && second == alSecond)
			cout << "WAKE UP!!!" << endl;
			//TODO Code for sound here
			//cout << '\a';
			Beep(523, 500);
	}
Has your alarm.h file changed? If so, please post it.

Line 9,12: What is the state of alarmOff? Hint: It's uninitialized (garbage).

Line 44: You have no argument name.

Lines 45-47: You're confusing argument names and member variables names again. They must be different.

Line 61: What if the current hour is 12? You're going to throw an exception. Also you have no provision from rolling over from am to pm, or pm to am.

Line 69: Stylistic suggestion. This is not a getter. You're not returning the value of am. The get prefix is a misnomer. For testing a condition, I prefer isAM().

Line 99: You're calling a bool function and ignoring the result. This is why I don't like the name getAM(). If you want to prompt the user for input, I suggest using prompt as the function prefix.

Line 100: You're declaring and setting a local variable that hides the member veriable by the same name.

Line 106: Why do you have a call to wakeUp() inside displayAlarm()? The wakeUp function has nothing to do with displaying the alarm time.

Line 110: This function ignores the state of alarmOff.

Line 110-115: This is going to test if the times are the same exactly once and then exit. You want a loop and to call Sleep()* in the loop if the alarm time has not been reached.
* Sleep() is an implementation defined function and not part of the C++ library.

Line 111-114: Only the cout is within the range of the if statement. The Beep() is going to be executed unconditionally. You want {} around both the cout and the Beep().

You don't appear to be updating hour, miinute or second anywhere in your program
closed account (4Eh5oG1T)
So 'm still pretty stumped, I realize theres something wong somewhere but...frankly i have no idea!

My Alarm.h
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
//#define ALARMCLOCK_H
class alarmClock {
	public:
		alarmClock();
		alarmClock(int, int, int, char);

		void setTime(int, int, int, char);
		void setHour(int);
		void setMin(int);
		void setSecond(int);
		void setAM(char);

		alarmClock& operator++();
		int getHour();
		int getMinute();
		int getSecond();
		char isAM();

		void displayTime();
		void setAlarm();
		void displayAlarm();
		void wakeUp();

	 private:
		int hour, minute, second;
		int alHour, alMinute, alSecond;
		char am;
		bool alarmState;
};


Alarm.cpp
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
#include "Alarm.h"
#include <iostream>
#include <iomanip>
using namespace std;
#include <stdexcept>
#include <windows.h>

alarmClock::alarmClock() {
	setTime(12, 0, 0,'A');
}

alarmClock::alarmClock(int hour, int minute, int second, char am) {
	setTime(hour, minute, second, am);
}

void alarmClock::setTime(int hour, int minute, int second, char am) {
	setHour(hour);
	setMin(minute);
	setSecond(second);
	setAM(am);
}

void alarmClock::setHour(int hr) {
	if (hr >= 1 && hr <= 13) {
		hour = hr;
	} else {
		throw invalid_argument("Hour must be 1-12");
	}
}

void alarmClock::setMin(int min) {
	if (min >= 0 && min < 60) {
		minute = min;
	} else {
		throw invalid_argument("Minute must be 0-59");
	}
}

void alarmClock::setSecond(int sec) {
	if (sec >= 0 && sec < 60) {
		second = sec;
	} else {
		throw invalid_argument("Second must be 0-59");
	}
}

void alarmClock::setAM(char am) {
	if (this->am == 'A') {
		this->am = 'A';
	} else {
		this->am = 'P';
	}
}

alarmClock& alarmClock::operator++() {
	if (second != 59) {
		++second;
	} else {
		second = 0;
		if (minute != 59) {
			++minute;
		} else {
			minute = 0;
			if (hour != 13) {
				++hour;
			} else {
				hour = 1;
				//If its AM then it rolls over to PM...or vice versa
				if (am = 'A') {
					am = 'P';
				} else {
					am = 'A';
				}
			}
		}
	}
	return *this;
}

	int alarmClock::getHour() {
		return hour;
	}

	int alarmClock::getMinute() {
		return minute;
	}

	int alarmClock::getSecond() {
		return second;
	}

	char alarmClock::isAM() {
		return (am == 'A');
	}

	void alarmClock::displayTime() {
		cout << setfill('0') << setw(2) << hour << ':' << setfill('0') << setw(2)
		<< minute << ':' << setfill('0') << setw(2) << second << ' ' << am << endl;
	}

	void alarmClock::setAlarm() {
		char dot = ':';
		cout << "Please, enter the alarm time in the form 12:00:00 AM:\n";
		cout << "Hour:";
		cin >> alHour;
		cin.get(dot);
		cout << "Minute:";
		cin >> alMinute;
		cin.get(dot);
		cout << "Second:";
		cin >> alSecond;
		if (alarmState != true || alarmState != false) {
			cout << "Alarm state on true? (Answer true or false)";
			cin >> alarmState;
		}
		isAM();
	}

	void alarmClock::displayAlarm() {
		cout << "Alarm time was set on " << setfill('0') << setw(2) << alHour << ':' << setfill('0') << setw(2)
		<< alMinute << ':' << setfill('0') << setw(2) << alSecond << ' ' << am << endl;
	}

	void alarmClock::wakeUp() {
		cout << this->hour;
		cout << " ";
		cout << this->minute;
		cout << " ";
		cout << this->second;
		cout << " ";
		cout << alHour;
		cout << " ";
		cout << alMinute;
		cout << " ";
		cout << alSecond;

		if (this->hour == alHour && this->minute == alMinute && this->second == alSecond && alarmState == true) {
			cout << "WAKE UP!!!" << endl;
			cout << '\a';
			Beep(523, 500);
			
		}
		system("PAUSE");
		cout << "Bitch";
	}


Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include "Alarm.h"
using namespace std;

int main() {
	int counter = 59;
	alarmClock Time;
	alarmClock Alarm;
	for (int i = 0; i < counter; i++) {
		//Seconds go up here second++
		Alarm.setSecond(counter);
		Alarm.operator++();
	}
	Alarm.displayTime();
	Alarm.setAlarm();
	Alarm.displayAlarm();
	Alarm.wakeUp();
};


I guess im having issues with actually incrementing the time, and any help would be greatly appreciated!
if you want it to work, you should read every single line and all of them should make sense.
Examples:
- Main.cpp, line 7: what is the purpose of variable Time?
- Main.cpp, line 11: why are you assigning every time the same value? Maybe you wanted to write Alarm.setSecond(i);?
- Main.cpp, line 12: why are you using the operator++ explicitly? It would be better to write ++Alarm;
- Main.cpp, lines 9-13; what is the purpose of the for loop?
- Alarm.cpp, line 111, you wrote
Please, enter the alarm time in the form 12:00:00 AM
, but actually you are requesting Hour, Minute and Second separately, and you are not requiring AM/PM
- Alarm.cpp, line 116, what is the purpose of isAM();?
- You are not initializing all member variables of alarmClock class
- Main.cpp, lines 14-17; what is the purpose of each line?

I guess im having issues with actually incrementing the time
What are you referring to? Why would you increment the time in this case?
closed account (4Eh5oG1T)
I'm talking about incrementing the time so that the default time (12:00:00) will reach whatever the user inputs as the alarm time, If that made any sense!
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
if (am == 'A') // <--
{
	am = 'P';
}
else
{
        am = 'A';
}


The formatting of the original statement is cute but not very good programming practice from the point of view of clarity and maintainability.

There seems to be a lot of trouble revolving around using char values A and P for the sake of just using a string value AM and PM. All the effort is wasted and is far too complex. In any case the time can be recorded in the class attribute as 24 hour clock time and write a method to convert. If the time_hour > 12 time_suffix is PM otherwise AM !!
Last edited on
closed account (48T7M4Gy)
Forgetting about Noon and Midnight, aTime.isAM() = true if time_hour <=12.

I'm talking about incrementing the time so that the default time (12:00:00) will reach whatever the user inputs as the alarm time, If that made any sense!
I still don't understand.
If you refer to Main.ccp, lines 9-13, you are incrementing 59 seconds (in an odd way), and before requesting input to the user.
Maybe you would increment time in the function wakeUp?
And the other issues are still there. Are you going to fix them?
You should revise your code and think about the purpose of each variable and each function. It would be better to use comments to help us (and maybe you) to understand what you are trying to do.
closed account (48T7M4Gy)
It's not a huge amount of work but it would pay off if two classes Time and Clock were used instead of just Time. Clocks read the time and have alarms, Times don't they only have hours, minutes, seconds and AM or PM (sometimes) :)
Topic archived. No new replies allowed.