Thread: switch problem

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    1

    switch problem

    Hi i am writing a program for class that requires the use of an array and switch. right now for some reason it only is pulling from the first value of the array for the switch and is always running the default before and after it runs the case. I am getting a warning saying the cases are the same. here is pretty much what it looks like.
    Code:
    "#include<string.h>
    #include <stdio.h>
    int main(void)
    {
    int a;
    int vote[50] =
    {9,3,10,6,55,9,7,3,3,27,15,4,4,21,11,7,6,8,9,4,10,12,17,10,6,11,3,5,5,4,15,5,31,15,3,20,7,7,21,4$
    char state[2];
    
            printf(" Please enter a state abv in lower case or EOF
    to quit\n");
    
            while((state[2] = getchar()) != 99)
            {
                    switch(state[2])
                    {
                    case 'al':
                            printf("The State of alabama has %d electoral votes\n",vote[0]);
                            break;
    
                     case 'ak':
                            printf("The State of alaska has %d electoral votes\n",vote[1]);
                            break;
                     case 'az':
                            printf("The State of arizona has %d electoral votes\n",vote[2]);
                            break;
    
                     case 'ar':
                            printf("The State of arkansas has %d electoral votes\n",vote[3]);
                            break;
    
                     case 'ca':
                            printf("The State of california has %d electoral votes\n",vote[4]);
                            break;
    
                     case 'co':
                            printf("The State of colorado has %d electoral votes\n",vote[5]);
                            break;
    
                     default:
                            printf("error\n");
                            break;
    
                    }
    
            }
    
    return 0;
    }"

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    state[2] does not exist, since the state array consists of state[0] and state[1]. Also the values of 'al', 'ak', etc. are implementation defined, but are guaranteed to not match whatever is in your state array. (Edit: I suppose I should mention that it is just plain not possible to switch on a string (array of characters).) And what is 99?
    Last edited by tabstop; 11-08-2008 at 12:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 04:05 PM
  2. problem on switch
    By toxicherry in forum C Programming
    Replies: 11
    Last Post: 12-31-2007, 05:17 AM
  3. Switch Problem
    By Tynnhammar in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2004, 11:57 AM
  4. Replies: 1
    Last Post: 08-31-2004, 04:07 AM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM