![]() |
|
#1
|
|||
|
|||
1st of the Monthi'm writing a program to allow the user to input a year, and from that i need to print out each month of that year. does anybody know a formula or a way to find out what day the first of each month is?
thanks for you help! |
|||
|
#2
|
||||
|
||||
|
I just had to answer this one. I was playing around with dates a while ago and stumbled across the time library. It does all the work for you. Basically, the time library is constructed around a structure called tm defined as (copied from the man page for mktime):
Code:
Now, the coolest thing about this structure, is that when you make a processing call to it, it will update the incorrect values properly. So if I put in for the year: 2004, the month: January (0) and the day (1027) and then call a process on it, it will automatically update all of the fields to give you the correct Month, Year, Date, Day of the week, etc for the day that is 1000 days from today (Jan. 27, 2004). Downside: The year is tracked as the number of years after 1900. So this structure is invalid for dates before 1900. (I am sure that there is a work around for this, I am just not smart enough to figure it out.) Anyway, here is a small program that will print a calendar for any year that is input: CPP / C++ / C Code:
Here is the first few lines from running this program: Code:
Anyway, the time library is very helpful in a lot of things like this. PLEASE NOTE: This was compiled using gcc under linux. I think that the time library is a standard ansi library and this should compile *anywhere*. If anyone compiles it on something different, let me know. |
|
#3
|
||||
|
||||
|
i compile this
it keep looping din print out anything is it got anythings wrong above ur program |
|
#4
|
||||
|
||||
|
Tay, I compiled this under linux using gcc. For curiosity sake, I tried to compile under Visual C++ 6.0 in Windows XP. First of all, it wouldn't compile without adding:
CPP / C++ / C Code:
Second, Visual C++ was more sensitive to the contents of the tm structure. While it is frustrating, it is my own fault for not being more careful about making assumptions. When I called mktime with the original program, I never set the other variables of the tm structures, so they were loaded with garbage. These were mostly the time containers. gcc still ran mktime, under Visual C++, it returns an error. To avoid this, I changed the first part of my do_calendar function to: CPP / C++ / C Code:
First off, the function returns an int instead of nothing. This will allow me to capture errors when mktime is called. Second, I preload the tm structure with the current date & time, so that all of the variables of the structure are valid values. Then in the function, where I call mktime, I changed it to: CPP / C++ / C Code:
Finally, to call the do_calendar function should now be something like the following: CPP / C++ / C Code:
One other disheartening thing that I learned about mktime is that it is only valid for the years 1970-2036. I have attached the complete revised source code below. If any one gets it running on something else, please let me know. This now works with both gcc/linux and Visual C++/win32. Remember this is a console program as well. |
|
#5
|
||||
|
||||
|
it can compile de.
it is a very good programm :-o thx you share this programing |
|
#6
|
||||
|
||||
|
Tay.
I am glad to hear that it compiled for you. Portability, even on a small program, can be a pain! I am curious if you don't mind, what compiler/OS are you using? |
|
#7
|
||||
|
||||
|
i use win XP Home edition
the compiler is dev c++ it is a very good compiler u can try to use it u can get it at this web: http://www.bloodshed.net/devcpp.html it is free software and support many library file like microsoft c++ did not support conio but dev c++ support it so u can play text and background color Quote:
|
|
#8
|
||||
|
||||
|
Tay.
Thanks for the link. I downloaded this and I am very impressed with it. It is based upon the gcc compiler which I am very used to. On a side note, I really try to avoid conio. It is not a standard library and so it makes portability a pain. The same is true with the *nix library called ncurses. Actually, anytime I need an interface, I am using a gui toolkit called FLTK. This stands for fast light tool kit it is extremely portable, compiles into small code and runs very fast. If you do much interface programming you may want to take a look at it. |
|
#9
|
||||
|
||||
|
dsmith
u know how to use fltk rite?? can u teach me how to use it with c++ code together and which compiler u use together with fltk? do u know how to include fltk inside the dev-c++ can u teach me thx |
|
#10
|
||||
|
||||
|
Hi Tay. Have you seen fltk before? I have actually only used it to date in linux under gcc. It is really pretty simple to use. I use the fluid dialog editor to get a basic setup that I like and then write out the code.
I will definitely give it a try in dev-c++ and let you know if it is as simple. If you would like to see any code before then, let me know and I will upload some for you to take a look at. |
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How do I get 'last month' in YYYYmm format? | JdS | MySQL / PHP Forum | 6 | 25-Mar-2009 03:26 |
| 150 mb storage only $3.30 a month! | WebServeHosting | Web Hosting Advertisements & Offers | 0 | 25-Nov-2003 01:28 |
| Free 1st month / Free setup / No credit card needed...Plans start at 4.95 | LarryIsaac | Web Hosting Advertisements & Offers | 0 | 11-Oct-2003 14:03 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The