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