Alarm Clock Program

Hello everyone. I tried to solve this problem in my last post but no luck and simplified the coding so that I could find the problem yet it is still not working. Here is the simplified code:

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
/* ctime example */
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <string>

using namespace std;

int main ()
{
	cout << "\nReminder\n";
	string inputTask;
	string inputTime;
	cout << "Remind myself to: ";
	getline(cin, inputTask);
	cout << "At this time (eg. Www Mmm dd hh:mm:ss yyyy : ";
	getline(cin, inputTime);
	
	bool loop = true;
	while (loop) 
	{
		time_t rawtime;
		time ( &rawtime );
		if (ctime (&rawtime) == inputTime)
		{
			cout << inputTask;
			loop = false;
		}
	}
	return(0);
}


Basically I don't think it is printing the 'Task' as I don't think ctime (&rawtime) == inputTime ever becomes true. I don't know how to fix this. Could anyone please give me their opinion as to what is wrong and perhaps a tip on how to solve it! Thank you
Well, I would compare the raw times directly instead of the string representation. Which could be minutely different (e.g. an extra space somewhere) and hence not match.

(maybe in your case it is because getline includes the \n character at the end of the line?)

So convert the users input time to a raw time and compare these.

Also put sleep(1) or something in your loop (you want to thrash the CPU until the alarm goes off?)

Also when you are comparing the raw times use > instead of == (especially if you put a sleep in the loop)
I was hoping I could avoid converting the users inputs into raw times but I guess I will have to do that. Thank you very much with you help :)
I converted the user input into seconds and test to see if that was smaller than the actual time from the start of the day, however it is still not working. I even implemented a sleep(1). I can't see what could possibly be wrong any more.

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
/* ctime example */
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <string>

using namespace std;

int main ()
{
	cout << "\nReminder\n";
	string inputTask;
	string inputTime;
	cout << "Remind myself to: ";
	getline(cin, inputTask);
	cout << "At this time (eg. hh:mm:ss): ";
	getline(cin, inputTime);
	
	bool loop = true;
	while (loop) 
	{
		float seconds = time(NULL);
		while (seconds > 60*60*12) 
		{
			seconds -= 86400;
		}
		
		float inputSeconds = inputTime[0] * 36000 + inputTime[1] * 3600 + inputTime[3] * 600 + inputTime[4] * 60 + inputTime[6] * 10 + inputTime[7];
		
		if (seconds > inputSeconds)
		{
			cout << inputTask;
			loop = false;
		}
		sleep(1);
	}
	return(0);
}


Once again, any help would be appreciated!
Last edited on
You could try

strptime

http://linux.die.net/man/3/strptime

to convert the string to a time structure

and then

mktime

http://www.cplusplus.com/reference/clibrary/ctime/mktime/

to convert this structure to a raw time (this is the number of seconds that have passed since Jan 1st 1970 on unix/linux)

http://en.wikipedia.org/wiki/Unix_time

I admit the whole time thing is a mess, but this is definitely the way to go instead of trying to compare the strings

Topic archived. No new replies allowed.