Thread: switch problem

  1. #1
    Registered User
    Join Date
    Nov 2008

    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.
    #include <stdio.h>
    int main(void)
    int a;
    int vote[50] =
    char state[2];
            printf(" Please enter a state abv in lower case or EOF
    to quit\n");
            while((state[2] = getchar()) != 99)
                    case 'al':
                            printf("The State of alabama has %d electoral votes\n",vote[0]);
                     case 'ak':
                            printf("The State of alaska has %d electoral votes\n",vote[1]);
                     case 'az':
                            printf("The State of arizona has %d electoral votes\n",vote[2]);
                     case 'ar':
                            printf("The State of arkansas has %d electoral votes\n",vote[3]);
                     case 'ca':
                            printf("The State of california has %d electoral votes\n",vote[4]);
                     case 'co':
                            printf("The State of colorado has %d electoral votes\n",vote[5]);
    return 0;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    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