-
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;
}"
-
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?