# Thread: Struc and Switch case

1. Bahh, sorry. Didn't see your post.

Alright, the code

Code:
`char months[12][] = {...};`
. . . will create an array of strings. The [] is just fancy notation for "determine the size of the array from the size of the initializer.".

Hope that helps slightly . . .

2. Oh another thing, you said I can declare my struct there as well.. so would I be able to do this?

Code:
```struct month const char monthchars[12][] = { "january", february, march, april, may, june, july, august, september, october, november, december } // of course each one has the  "" around them.

//then the for loop

int i=0,x;

for(i=0;i<12;i++)
{
if(!strcmp(month.monthEng, monthchars[i])
return i;
}
else
printf("Type in lower case please...");
return -1 //i had a question here, why return -1? what does that mean?```

3. Ah, cool. Awesome, easier that way haha. Okay... this is cool, learning so much haha and getting help at the same time. Like killing two birds with one stone.

4. Originally Posted by dlwlsdn
Oh another thing, you said I can declare my struct there as well.. so would I be able to do this?

Code:
```struct month const char monthchars[12][] = { "january", february, march, april, may, june, july, august, september, october, november, december } // of course each one has the  "" around them.

//then the for loop

int i=0,x;

for(i=0;i<12;i++)
{
if(!strcmp(month.monthEng, monthchars[i])
return i;
}
else
printf("Type in lower case please...");
return -1 //i had a question here, why return -1? what does that mean?```
Several problems that I can see. What I meant, is that you can pass your struct to the function instead of a string. Helps if you want to compare both the english and the spanish versions of the month string. (You also need a semicolon after the declaration . . .)

Basically, you're right. I say return -1 so you can check, in the place you called the function, to see if it had an error. I use -1, personally, since it's clearly not an array index. (Again, semicolon after the return statement. )

For example, if the function returns -1, then you know to loop back up and re-run the string entering code.

5. OoO return -1 is pretty handy , haha. Okay so when you say pass the struct to the function instead of a string.. do you mean something like...
Code:
```struct month
{
char monthEng[10];
char monthSpan[11];
int order;
int numberDays;
}

int find_the_day(struct month *pointer1, char *user, int x) /* pointer1 is pointing to the struc, user is what the user typed in for month, and x is the day the user typed in for days. */
{
.....
}```
or am i totally off again?

6. Yeah, that could work.

Something like (assuming that pointer1 points to the struct that you want to fill with the entered data)

Code:
```int find_the_day(struct month *pointer1, char *user, int x) {
int x;
for(x = 0; x < 12; x ++) {
if(strcmp(user, months[x]) == 0) {
strcpy(pointer1->monthEnd, user);
pointer1->numberDays = monthlengths[x];
}
}
return -1;
}```
Something similar, assuming that months is the list of months in string form, and monthlengths is the list of month lengths.

Now, why are you passing x?

EDIT: I check the return for strcmp() against 0, not against NULL. Salem just pointed out to me that using ! is incorrect. You shouldn't have any problems with it, though.

7. Okay, nice. Alrighty. I understand this, took me a while to get to used to the pointer, totally forgot that -> is a pointer to the struct member haha. okay

well to answer the question, the reason why we pass x is so that it will "march" through the array of strings or technically ints to find the right one that matches it. So if its april, it will "march" through the array until it finds april.

or am i wrong? lol

8. Err . . . what I mean, is, why do you call it " . . . x is the day the user typed in for days."

AFAICT, you won't need that. Try this . . .

Code:
```int find_the_number_of_days(struct month *pointer1, char *user) {
int x;
for(x = 0; x < 12; x ++) {
if(strcmp(user, months[x]) == 0) {
strcpy(pointer1->monthEnd, user);
pointer1->numberDays = monthlengths[x];
}
}
return -1;
}```
And tell me what it isn't doing. (I made a mistake in the last one . . . declared the variable x twice. My bad.)

9. what it isn't doing... hmm... Well its not find the day, at least thats I think... because pointer1->numberDays = monthlengths[x], means that the numberDays is going to equal monthlengths[x], but x won't go beyond 11 and we need to get 31, 28(29), 30, 31, 30, 31, 31, 30, 31, 30, 31, 30, 31 or something like that..

really not sure...

10. Well, if monthlengths[] is declared as {31, 28, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30, 31} . . . then it'll work except for the leap years. I believe I mentioned that earlier . . .

11. Originally Posted by dlwlsdn
const char monthschar[12][], why did you do a 2-dimension... I don't know 2-dimensions at all, or when to use them. haha, but i understand everything you wrote, except the 2-dimensions. Is it cause we are making it 12 elements long, and each element will have, the months you wrote?
You will have to use a two dimensional array to hold the list of months. const char monthchars[12][] means you will have an array of 12 strings, allocated as defined (hence [12][], like string[]="here"). You can reference each string in the array by number (january=0, december=11). Adding elements to a two-dimensional array is slightly more complicated, but that doesn't matter here since you already know the calendar and it won't change, so define it as a const.

EDIT: oops, just noticed I must have read only the first page...you guys have gotten way past this now

12. Yeah except you've missed one thing. It's
Code:
`const char monthchars[][10]`
not
Code:
`const char monthchars[12][]`
The later wont compile because only the leftmost array size can be left unspecified.
(10 is enough for the longest month name "september")

13. . . . good point . . .

Perhaps something more like [12][10]? The longest month name is "september", or 9 chars, plus a NULL equals 10.

My bad for that mistake.

14. You guys are too smart, haha. I understand now. Hmm... well I've been writing the code for a bit now, I'll post it once I'm finished and see what we can do if I run into something. Thank you guys very much for helping me and giving me your insights, its been very helpful to me You guys are too awesome for me. haha ^^

15. We have the benefit of experience. You don't yet.

Let us know how it goes.

Popular pages Recent additions