I have developed a C++ app to print Hello at 23:01:50
Here is my code
#include<iostream>
#include<string>
#include <time.h>
#include <windows.h>
using namespace std;
int main ()
{
time_t start = time (&start);
cout<<ctime(&start);
while(1)
{
time (&start);
if( ctime(&start) == "Fri Jan 18 23:01:50 2013\n" )
cout << "Hello";
Sleep(500);
cout << ctime(&start);
}
}
But output is:
Fri Jan 18 23:01:49 2013
Fri Jan 18 23:01:49 2013
Fri Jan 18 23:01:50 2013
Fri Jan 18 23:01:50 2013
Fri Jan 18 23:01:51 2013
Fri Jan 18 23:01:51 2013
Why Hello is not printed?
Thanks
In C++ the equals operator “==” does not work as you expect for char pointers (which is what you are using). It is comparing the pointers which are actually pointing to different places in memory. To compare the strings (which is not the best way to do this check BTW) you will need to use a string comparison function.
For example:
For more info see: http://www.cplusplus.com/reference/cstring/strcmp/