Thread: some help on structures!!

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    5

    some help on structures!!

    hi i have a problem trying to initialize the members of the structure...i am getting errors i don't really know what to do next...could someone please help me solve this problem. what the program is suppose to do is to print the whole array of a calendar year showing the month, day and the number of that month when the user enters the value one

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define DAYS 12
    #define MONTH 12
    
    struct cal  {
            int number;
            char months[MONTH][MONTH];
            int days[DAYS];
    };
    
    int main(void){
    
    struct cal library = {.months[MONTH][MONTH] = {{"jan"}, {"feb"}, {"mar"}, {"apr"}, {"may"}, {"jun"}, {"jul"}, {"aug"}, {"sep"}, {"oct"}, {"nov"}, {"dec"}},
                            .days[DAYS] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    }
    int index;
    
    int menu;
    printf("The menu options\n");
    printf("1. Print Array\n");
    printf("2. Set Days for a Month\n");
    printf("3. Display Days for a Month\n");
    printf("4. Quit\n");
    
    printf("Please enter the option you want to proceed with\n");
    scanf("%i", &menu);
    
            if(menu==1)
            {
                    for (index =0; index < DAYS && index < MONTH; index++){
                                    printf("Month %2d which is %s has %2d days\n", index +1, library.months[index], library.days[index]);
                            continue;
                            }
    
            }
    
    
            else
                    printf("No good");
    
            return 0;
    
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What does "set days for a month" actually mean?

    Also:
    Code:
    #define DAYS 12
    #define MONTH 12
    
    struct cal  {
            int number;
            char months[MONTH][MONTH];
            int days[DAYS];
    };
    Why is "months[MONTH][MONTH]"? As far as I know, the length of the name of a "month" string is not actually related to the NUMBER OF MONTHS.
    Also, days _IS_ related to the number of months, so why have a different definition for that - surely the number of days per month is exactly one per month, right?

    What is "int number" used for?

    Further, I would probably declared an struct that has the name of the month and number of days as the members and make an array of that struct, rather than have two arrays in one struct.

    Code:
    index < DAYS && index < MONTH
    That's a bit overly complex way to check if index is less than 12, don't you think - no, I'm not suggesting you put 12 in there, but rather that you only check one of DAYS or MONTH, since they are the same [which would happen automatically if you do what I suggested above]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    5
    the program is a menu option where the user can choose to perform 1 of 4 tasks...but the program that you are seeing right now is unfinished (in terms of adding the other menu options)

    the "set days for a month" means that the user can enter either the month or the number that correlates to the month and from there change the number of days associated with that month.

    the output of this program (at least for option 1) is suppose to look something like this ...
    Code:
    [shrc0128@munro LAB8]$ a.out
    The menu options
    1. Print Array
    2. Set Days for a Month
    3. Display Days for a Month
    4. Quit
    Please enter the option you want to proceed with
    1
    Month  1 which is jan has 31 days
    Month  2 which is feb has 28 days
    Month  3 which is mar has 31 days
    Month  4 which is apr has 30 days
    Month  5 which is may has 31 days
    Month  6 which is jun has 30 days
    Month  7 which is jul has 31 days
    Month  8 which is aug has 31 days
    Month  9 which is sep has 30 days
    Month 10 which is oct has 31 days
    Month 11 which is nov has 30 days
    Month 12 which is dec has 31 days
    [shrc0128@munro LAB8]$

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    As a start (following matsp's suggestion regarding the struct and array), create a "month" struct:
    Code:
    struct month
    {
        const char* month_name;
        int num_days;
    };
    Then create an array of those structs and initialize:
    Code:
    struct month calendar[] = {{"jan",31},{"feb",28},{"mar",31},{"apr",30},{"may",31},{"jun",30},
                               {"jul",31},{"aug",31},{"sep",30},{"oct",31},{"nov",30},{"dec",31}};
    const int num_months = sizeof(calendar) / sizeof(*calendar);
    That should get you over your "initialization" hump so you can work from there.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    5
    nice....thanks you guys for all your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures within Structures
    By Misko82 in forum C Programming
    Replies: 2
    Last Post: 08-27-2007, 12:25 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM