Thread: Struc and Switch case

  1. #16
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    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. #17
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    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. #18
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    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. #19
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Quote Originally Posted by dlwlsdn View Post
    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. #20
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    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. #21
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    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.
    Last edited by Nightowl; 11-21-2008 at 12:00 PM.

  7. #22
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    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. #23
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    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. #24
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    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. #25
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    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 . . .
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  11. #26
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by dlwlsdn View Post
    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
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #27
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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")
    Last edited by iMalc; 11-21-2008 at 01:19 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #28
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    . . . 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.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  14. #29
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    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. #30
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    We have the benefit of experience. You don't yet.

    Let us know how it goes.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

Popular pages Recent additions subscribe to a feed