Arrays and Switches

This is a discussion on Arrays and Switches within the C Programming forums, part of the General Programming Boards category; I don't need this program anymore, but it still makes me mad. I tried it with a low of if ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    13

    Arrays and Switches

    I don't need this program anymore, but it still makes me mad. I tried it with a low of if and else if statements, and now I've tried this switch case, and neither worked. Any help?

    Code:
    #include <stdio.h>
    
    
    int main() {
    
        char ppart[40];
        scanf("%s", ppart);
        while(ppart > 0) {
            printf("Enter a part type:");
            switch(ppart) {
                case "noun1":
                printf("Light\n");
                break;
                case "verb":
                printf("Walking\n");
                break;
                case "adjective":
                printf("Dark\n");
                break;
                case "color":
                printf("Blue\n");
                break;
                case "sound":
                printf("Quiet\n");
                break;
                case "simile":
                printf("Paths like tree farm rows\n");
                break;
                case "emotion":
                printf("Strong lonliness\n");
                break;
                case "noun2":
                printf("Grass");
                break;
            }
    
            return 0;
        }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You can't use strings in the switch statement... use strcmp()

    Code:
    /* take the newline character off what the user entered (see the FAQ) */
    scanf("&#37;[^\n]", ppart);
    
    /* ... */
    
    if(strcmp(ppart, "noun1") == 0)
    {
        printf("Light\n");
    }else if(strcmp(ppart, "verb") == 0){
        printf("Walking");
    }else{
        printf("I don't know\n");
    }
    Then again, use fgets() to read the string from the user and strip off the newline to avoid buffer overflow.

    All of this can be found in the FAQ.
    Last edited by zacs7; 04-21-2008 at 09:50 PM.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
        while(ppart > 0) {
    Whilst this is valid C, it's likely to not be particularly useful. ppart here would be the location on of the array, which may or may not be "greater than zero" (because pointers converted to integer is a signed value, and if it so happens that the address has the highest bit set, it will be negative - for example in Linux this would be the case, because the stack starts at just under 3GB, which is over the 2GB "sign" barrier) - but it certainly won't change once the ppart has been created. If you don't follow that, just take the fact that "the above code doesn't do anything meaningful", and if you can't figure out how to do something useful instead, explain what you meant to do, and we will explain how to do that.

    Next, aside from a chain of strcmp(), a possible solution would be:
    Code:
    struct pair
    {
        char *part;
        char *word;
    } pairs[maxparts] = { 
         { "noun1",  "Light" },
         { "verb", "Walking" },
         { "adjective", "Dark" },
         { "color", "Blue" },
         { "sound", "Quiet" },
         { "simile", "Paths like tree farm rows" },
         { "emotion", "Strong lonliness" },
         { "noun2", "Grass" }
    };
    
    const int maxparts = sizeof(pairs) / sizeof(pairs[0]);
    
    int main()
    {
        int i;
    ....
        for(i = 0; i < maxparts; i++)
        {
            if (strcmp(pairs[i].part, ppart) == 0)
            {
                printf("%s\n", pairs[i].word);
                break;   // Found it. 
            }
        }
    ...
    Also, in your code, you have the printf to ask for a part and the scanf to read it in swapped around.

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

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    13

    Matsp's code

    I don't understand that code, I haven't finished the C tutorials yet.
    I tried to use a bunch of if()'s at one time, however it only printf()'d my prompt, and when I entered anything, rather than scanning it to the array and reading back the answers, it just reprompted me.

    The while() statement is just a loop that I put meaningless code it to make it go. I just didn't want the program to terminate every time I entered a request.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    13

    Lightbulb What I meant to do

    Oh, and yeah, what I meant to do.

    I wanted to have it return the appropriate word when the word type (verb, noun1) was entered.

    I had some homework, and I thought I'd write a quick program to bring up my word list as I requested each word, but of course the one time I try to help myself with C I fail.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do switches and arrays mix?
    By crag2804 in forum C Programming
    Replies: 1
    Last Post: 10-27-2002, 03:05 PM
  2. Arrays
    By yoda_21 in forum C Programming
    Replies: 2
    Last Post: 01-27-2002, 11:57 AM

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