# Thread: some help on structures!!

1. ## 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. 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

3. 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. 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.

5. nice....thanks you guys for all your help

Popular pages Recent additions