Author Topic: [C] A date countdown  (Read 6732 times)

0 Members and 1 Guest are viewing this topic.

Offline deadly7

  • 42
  • x86
  • Hero Member
  • *****
  • Posts: 6496
    • View Profile
[C] A date countdown
« on: April 23, 2010, 11:47:30 pm »
I'm trying to define a date and give it the following properties:

-First generate a number value based off of it (e.g. give it a date "14")
-Calculate a figuer 20-x where x is today's current date
-Refresh thsi cycle every 2 weeks, so basically define "20-x" as a variable and then reset the value of x to be 0 when 20-x =0

Here's overall what I'm trying to do, I'd appreciate feedback on if this would be a smart way to go about it:
Give a specific date (e.g. 4/23/2010) a specific a number of days away from today
Count down to the date with a value 14-x
if "14-x" = 0 then display a text message AND reset the value of x = 0
Constantly keep this loop going
Also, I'd like it if it was always on and would pop up a cmd.exe window even if I didn't run it manually.

Some of this I already know how to do or can more easily figure out (e.g. setting the value of x, the if/then statement) but I can't seem to figure out how to quantify the date to figure this out so that x moves every day to be x=x+1


--------------------
I'm creating a program that repeatedly counts down 2 week intervals from a certain date (specifically 4/23/2010 and every 2 weeks after that). To that extent, I'm trying to assign the value of that date to an integer--this is the first thing I need help doing. After I assign the value of that date to an integer, I'm trying to run the calculation "14-x" where 14 is the number of days left until the program should alert me. x is supposed to be an integer that is given the value x=x+1 when every new day occurs (e.g. it will start at 0 on 4/23/2010 and then go up to 1 on 4/24/2010 , etc). I also can't figure out how to increment x with each day--this is the second thing I need help with. The last part of the program would then be a loop that checks if the value of "14-x" is 0 or not; if it is 0 then it will reset the value of x to 0 and have it countdown again. When the value of x is RESET to 0 the program should also pop up with whatever notification I want to give it and hold the screen until getkeypress(); is run. I also want the program to be running indefinitely in the background--this is the third thing I can't get.
« Last Edit: April 26, 2010, 09:15:01 pm by deadly7 »
[17:42:21.609] <Ergot> Kutsuju you're girlfrieds pussy must be a 403 error for you
 [17:42:25.585] <Ergot> FORBIDDEN

on IRC playing T&T++
<iago> He is unarmed
<Hitmen> he has no arms?!

on AIM with a drunk mythix:
(00:50:05) Mythix: Deadly
(00:50:11) Mythix: I'm going to fuck that red dot out of your head.
(00:50:15) Mythix: with my nine

Offline while1

  • x86
  • Hero Member
  • *****
  • Posts: 1013
    • View Profile
Re: [C] A date countdown
« Reply #1 on: April 25, 2010, 08:33:55 pm »
I'm assuming you're using Windows and a Visual C/C++ compiler? Since you mention cmd.exe and all.

Anyways, since you're not using .NET, check out some of the information here about the ANSI C Standard Library and Win32 API (not sure what different for developers on Win7 or Vista):

http://www.codeproject.com/KB/datetime/datetimedisc.aspx
http://msdn.microsoft.com/en-us/library/ms724338.aspx

In that loop you mention, your program could could be polling the current local/system date/time and use that to determine when to increment x.

« Last Edit: April 25, 2010, 08:37:55 pm by while1 »
I tend to edit my topics and replies frequently.

http://www.operationsmile.org

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [C] A date countdown
« Reply #2 on: April 26, 2010, 03:46:58 pm »
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [C] A date countdown
« Reply #3 on: April 26, 2010, 04:17:51 pm »
Were you drunk when you wrote this post, deadly?  Hahaha... it's not really clear to me what you're trying to do.  I think it's much better to clearly state the problem you're trying to solve instead of mixing in potential solutions with the problem.

Offline while1

  • x86
  • Hero Member
  • *****
  • Posts: 1013
    • View Profile
Re: [C] A date countdown
« Reply #4 on: April 26, 2010, 06:24:51 pm »
Were you drunk when you wrote this post, deadly?  Hahaha... it's not really clear to me what you're trying to do.  I think it's much better to clearly state the problem you're trying to solve instead of mixing in potential solutions with the problem.

Aye, I found it difficult to understand what he was asking as well.  Not even sure exactly what the program he's writing is for exactly (why he is using some seemingly arbitrary constants i.e. 20).

I was just throwing shit at the wall and seeing if it would help.
I tend to edit my topics and replies frequently.

http://www.operationsmile.org

Offline deadly7

  • 42
  • x86
  • Hero Member
  • *****
  • Posts: 6496
    • View Profile
Re: [C] A date countdown
« Reply #5 on: April 26, 2010, 09:14:39 pm »
Were you drunk when you wrote this post, deadly?  Hahaha... it's not really clear to me what you're trying to do.  I think it's much better to clearly state the problem you're trying to solve instead of mixing in potential solutions with the problem.
Wow. I reread this post now and fail completely. Hahahaha. Let's try this instead:


---------------

I'm creating a program that repeatedly counts down 2 week intervals from a certain date (specifically 4/23/2010 and every 2 weeks after that). To that extent, I'm trying to assign the value of that date to an integer--this is the first thing I need help doing. After I assign the value of that date to an integer, I'm trying to run the calculation "14-x" where 14 is the number of days left until the program should alert me. x is supposed to be an integer that is given the value x=x+1 when every new day occurs (e.g. it will start at 0 on 4/23/2010 and then go up to 1 on 4/24/2010 , etc). I also can't figure out how to increment x with each day--this is the second thing I need help with. The last part of the program would then be a loop that checks if the value of "14-x" is 0 or not; if it is 0 then it will reset the value of x to 0 and have it countdown again. When the value of x is RESET to 0 the program should also pop up with whatever notification I want to give it and hold the screen until getkeypress(); is run. I also want the program to be running indefinitely in the background--this is the third thing I can't get.

Sorry about my fail coherency. I do believe I was in a 7 hour meeting at that point...
[17:42:21.609] <Ergot> Kutsuju you're girlfrieds pussy must be a 403 error for you
 [17:42:25.585] <Ergot> FORBIDDEN

on IRC playing T&T++
<iago> He is unarmed
<Hitmen> he has no arms?!

on AIM with a drunk mythix:
(00:50:05) Mythix: Deadly
(00:50:11) Mythix: I'm going to fuck that red dot out of your head.
(00:50:15) Mythix: with my nine

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [C] A date countdown
« Reply #6 on: April 26, 2010, 09:30:36 pm »
Okay, that's better.  The next important question I have is what platform are you working on?

I assume this is an academic exercise?

Offline while1

  • x86
  • Hero Member
  • *****
  • Posts: 1013
    • View Profile
Re: [C] A date countdown
« Reply #7 on: April 26, 2010, 10:50:08 pm »
Ok, your algorithm is very simplistic, but without knowing the context for the reason you're writing it... simplistic may be all you need to do (i.e. if it's a personal project).

But let's assume that you implement all of these things, your algorithm is utterly useless if you restart your computer, your computer crashes, etc.

The next important question I have is what platform are you working on?

Probably irrelevant.

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

This should be more than what he needs accomplish his program.  And any platform with a standard C library should suffice.  It even has nice simple code examples on how to use the various functions and types.

The std C library functions in the link provides the means of getting the current system date/time (e.g. time()) and putting it into a form that you can use (e.g. localtime()), as well as converting between different time formats and manipulating them (e.g. converting a numerical date/time to its string representation).  You shouldn't even need to increment a separate x variable if you're taking the difference from the start date/time and the current local date/time being polled... taking this difference is effectively the same thing.  To not waste CPU cycles, you could even sleep for a bit in between polling so you aren't constantly making function calls to time() and localtime().  The linked webpage lists a function called difftime() to find the difference between two dates/times.

Using all of this, you could then have the program run as a background process and even have it start up when you boot up the OS, get the current date/ time, and read the last known specified start date from a file... With this information, it would be able to resume where it left off by computing the date/time difference of current date and the last known start date specified.  If the difference is more than 14 days (i.e. you turned off your computer for two weeks+) you would let the user know... otherwise you would just resume normal program execution until the current date/time is 14 days in the future from the last known specified start date/time.

I'm not sure what level of programming skill deadly is at... so throwing him a link may not be sufficient enough for him to practically apply the information in the link.  Let me know if I'm beating a dead horse.

Unless there is a requirement that running the process in the background must be implemented into the program/ binary, running the process in the background is easy in both Windows and Linux/UNIX and can be accomplished by just writing a shell script or batch file to accomplish this behavior.
« Last Edit: April 26, 2010, 11:40:27 pm by while1 »
I tend to edit my topics and replies frequently.

http://www.operationsmile.org

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: [C] A date countdown
« Reply #8 on: April 29, 2010, 11:56:54 pm »
Your post is one big block of text... impossible to read.

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [C] A date countdown
« Reply #9 on: April 30, 2010, 12:06:38 am »
Ok, your algorithm is very simplistic, but without knowing the context for the reason you're writing it... simplistic may be all you need to do (i.e. if it's a personal project).

But let's assume that you implement all of these things, your algorithm is utterly useless if you restart your computer, your computer crashes, etc.

I asked the question about it being an exercise because it seems like it's something you could accomplish with Google Calendar in a couple of minutes...

The next important question I have is what platform are you working on?

Probably irrelevant.

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

This should be more than what he needs accomplish his program.  And any platform with a standard C library should suffice.  It even has nice simple code examples on how to use the various functions and types.

The std C library functions in the link provides the means of getting the current system date/time (e.g. time()) and putting it into a form that you can use (e.g. localtime()), as well as converting between different time formats and manipulating them (e.g. converting a numerical date/time to its string representation).  You shouldn't even need to increment a separate x variable if you're taking the difference from the start date/time and the current local date/time being polled... taking this difference is effectively the same thing.  To not waste CPU cycles, you could even sleep for a bit in between polling so you aren't constantly making function calls to time() and localtime().  The linked webpage lists a function called difftime() to find the difference between two dates/times.

Using all of this, you could then have the program run as a background process and even have it start up when you boot up the OS, get the current date/ time, and read the last known specified start date from a file... With this information, it would be able to resume where it left off by computing the date/time difference of current date and the last known start date specified.  If the difference is more than 14 days (i.e. you turned off your computer for two weeks+) you would let the user know... otherwise you would just resume normal program execution until the current date/time is 14 days in the future from the last known specified start date/time.

I'm not sure what level of programming skill deadly is at... so throwing him a link may not be sufficient enough for him to practically apply the information in the link.  Let me know if I'm beating a dead horse.

Unless there is a requirement that running the process in the background must be implemented into the program/ binary, running the process in the background is easy in both Windows and Linux/UNIX and can be accomplished by just writing a shell script or batch file to accomplish this behavior.


wtf man... hahah.  that was one crazy wall of text.

I was meaning for 'platform' to include the language he's working in as well (which is usual, I think).  Maybe that's irrelevant functionally, but unless you're going to do some borked(© iago) system call or something, it's relevant to how you're going to code it.

Offline while1

  • x86
  • Hero Member
  • *****
  • Posts: 1013
    • View Profile
Re: [C] A date countdown
« Reply #10 on: May 01, 2010, 07:40:58 pm »
From the topic title, it's a good assumption that he's writing it in C or C++.  Since his original post mentioned cmd.exe, it's likely Windows.  Then again, I'm assuming all of this.

He can just use some nice conditional preprocessor directives if he wants to compile for multiple platforms.
« Last Edit: May 01, 2010, 07:44:09 pm by while1 »
I tend to edit my topics and replies frequently.

http://www.operationsmile.org

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [C] A date countdown
« Reply #11 on: May 01, 2010, 07:45:08 pm »
hahahaha...touche.  I guess I didn't pay any attention to the title. :)