Year, month and day

This is a discussion on Year, month and day within the C Programming forums, part of the General Programming Boards category; Hello, I have a question on how to represent day, month and year in C in the structure. I know ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    28

    Year, month and day

    Hello,
    I have a question on how to represent day, month and year in C in the structure. I know a month must be a string. I am not sure where to start. I just need some kind of directions....

    Thank you in advance!

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    enum Months {Jan, Feb, Mar, ..., Dec}
    struct Date
    {
       Months month;
       char Day;
       char Year;
    };
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Representing the year in unnecessarily small datatype is what caused the Y2K problems, Vart.
    Sent from my iPadŽ

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    ok - __int64 can be used... in this case the program will survive till the end of the Galaxy (I suppose)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    from looking at that code its hard to figure out what you want to do.
    heres some code that does somthing I guess:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct {
            int day;
            char month[10];
            int year;
    } date;
    
    int main(void) {
    
            date today;
    
            today.day = 30;
            today.year = 2006;
            strcpy(today.month,"November");
    
            printf("Today's date is  %s %d %d\n", today.month, today.day, today.year);
    
            return 0;
    }
    hopefully its remotly close to what your trying to accomplish.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    28
    Thank you for help...


    It might be a stupid question, but will this work out with any date?

  7. #7
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    No, what sl4nted supplied will only work for today. And I think what he's pointing out is that what you supplied gives us no indication as to what you're trying to do...
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    all this does is print the values in the structure, which I supplied

    Code:
            today.day = 30;
            today.year = 2006;
            strcpy(today.month,"November");
    does it work with any date?

    if you mean can you change those values to any date, yes you should be able to.
    the month can be 10 characters long, since I was too lazy to figure out which month has the most letters in it. And actually count how many. So as long as no month has more than 9 letters it should be fine.

  9. #9
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I think he was wondering if what you'd supplied would work for any date. And it won't. Regardless of the date, it'll always print out : "Today's date is November 20 2006".
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    28
    Yeah Happy_Reaper that is what I meant...
    Is there a way the program would print any date? You guys are awesome!
    Thank you for help...
    Last edited by Newbie999; 11-30-2006 at 09:31 PM.

  11. #11
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Yes. Look at time.h
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    28
    How about if the program just captures data(Prompts the user to enter any date) and outputs it by representing a date( day, month and year...)
    Thanks a lot!

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct {
            int day;
            char month[10];
            int year;
    } date;
    
    int main(void) {
    
            date today;
    
            printf("Enter the day> ");
            scanf("%d", &today.day);
    
            printf("Enter the month> ");
            scanf("%s", today.month);
    
            printf("Enter the year> ");
            scanf("%d", &today.year);
    
            printf("Today's date is  %s %d %d\n", today.month, today.day, today.year);
    
            return 0;
    }
    you should add tests of the input recieved, and read the faq for a better method of getting a string. this should get you going though, I'm lazy

    output:
    Enter the day> 30
    Enter the month> November
    Enter the year> 2006
    Today's date is November 30 2006

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    28
    Thank you!
    Yes, it seems the better way of using string is the only thing I will need!

  15. #15
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Even using scanf for integer input could lead to problems. Usually, a combination of fgets and sscanf is what you'd want.

    And sl4nted, usually the policy around here is not to do other people's code for them, especially when they have yet to post an attempt themselves.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calendar Problem
    By wordup in forum C Programming
    Replies: 7
    Last Post: 10-29-2002, 03:36 PM
  2. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 10:45 PM
  3. Printing weekday
    By sworc66 in forum C Programming
    Replies: 12
    Last Post: 09-13-2002, 08:03 AM
  4. debug program
    By new_c in forum C Programming
    Replies: 3
    Last Post: 03-18-2002, 11:50 PM
  5. Simplified code
    By soonerfan in forum C Programming
    Replies: 2
    Last Post: 12-05-2001, 03:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21