New programmer here. Like super new. I'm trying to write a program for my dad's birthday.
#include <stdio.h>
#include <time.h>
int main()
{
int CurrentTime;
system("start https://www.youtube.com/watch?v=FchMuPQOBwA");
return 0;
}
So far I have that. How do I do it so he is not able to open it before his birthday or specific time? Looked around in time.h and googled a bit but I can't seem to find a solution. Also how do I send it to him so it's just a .exe and he can't see the code?
Thanks in advance
The function sleep gives a simple way to make the program wait for a short interval. If your program doesn't use signals (except to terminate), then you can expect sleep to wait reliably throughout the specified interval.
The pause function suspends program execution until a signal arrives whose action is either to execute a handler function, or to terminate the process. If the signal causes a handler function to be executed, then pause returns.
Insert, wherever you need your program to make a delay:sleep(1000); Change the "1000" to the number of milliseconds you want to wait (for example, if you want to make a 2 second delay, replace it with "2000".
Using system(“pause”) command in C++ This is a Windows-specific command, which tells the OS to run the pause program. This program waits to be terminated, and halts the exceution of the parent C++ program. Only after the pause program is terminated, will the original program continue.
There is an example of all the functions you'll need to use in the reference of the "time()" function. http://www.cplusplus.com/reference/ctime/time/
The steps for what you need to do:
Here is a sample program:
#include <stdio.h> /* printf */
#include <time.h> /* time_t, struct tm, difftime, time, mktime */
int main ()
{
time_t c_time, b_time;
struct tm b_date;
double seconds;
y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0;
y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1; //January first, 2000. I'll let you change that because I don't know when's the big day.
time(&c_time); /* get current time; same as: timer = time(NULL) */
b_time = mktime(&b_date);
seconds = difftime(c_time,b_time);
if(seconds < 0) //negative difference means c_time > b_time
{
//do stuff
}
return 0;
}
Now, if you're a complete beginner, some of the stuff here is a bit hard to understand. I can only recommend that you read a good C tutorial, everything will become clear. I hope you have enough time ;)
On Windows (as you seem to use that OS), you can do something like this:
#include <windows.h>
#include <stdio.h>
/* The date on which this program should continue running */
#define DAY 10
#define MONTH 12 /* 1 = Jan... 12 = Dec */
int main()
{
SYSTEMTIME t;
GetLocalTime(&t);
if (t.wDay != DAY || t.wMonth != MONTH) {
printf("You can't open this program today!\n");
MessageBox(0, "You can't open this program today!", "Error", MB_ICONSTOP);
return 1;
}
system("start https://www.youtube.com/watch?v=FchMuPQOBwA");
return 0;
}
The GetLocalTime() function and SYSTEMTIME structure are in windows.h, which you thus need to include.
Or alternatively use the time() function from time.h but in that case you need to convert the desired day to a UNIX timestamp (see http://en.wikipedia.org/wiki/Unix_time), or to convert the info returned by time() into day/month.
This was for a simple program that will run only on that specific day, and exit with an error message if not. If you want to make a program that installs in his computer when he runs it and then waits for the specific time before opening the web page, that's much more complex (you'd basically have to copy the EXE file somewhere in the system, and add it to the registry so it runs automatically at login... probably not more than 30 lines of code, but not the simplest one ;-) ).
To send it as an EXE so he doesn't see the source code, well, you just have to compile it as if you want to run it, and then send the EXE to him (making sure it doesn't require runtime libraries from the compiler, check on another PC if it runs correctly). Of course if your dad looks inside the EXE file with some editor, he will see the address of the webpage (but not easily what is the condition that makes this page open).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With