Originally Posted by
hiven
Code:
char c=0;
int letter [3];
letter[0]=0; letter[1]=0; letter[2]=0;
printf("please enter a letter");
scanf("%c", &c);
switch (c) {
case 'a': letter[0]++;
case 'b': letter[1]++;
default: letter[2]++;
}
printf("%d %d %d", letter[0], letter[1], letter[2]);
Can someone please explain what line2 is actually doing. Is it there to define how many "letter" variations there are (i.e letter[0]...), or does it do something else?
Also because letter[0]=0 for example isnt given a value type, but it follows on from an int being defined, is it reasonable to assume letter[0] is being defined as an int as well?
Your code is really ate up. I'm not really sure what your trying to acheive.
Code:
#include <stdio.h>
int main(void)
{
char c;
int letter [3] = {0};
printf("please enter \"a\" or \"b\": ");
scanf("%c", &c);
switch (c)
{
case 'a':
++letter[0];
break;
case 'b':
++letter[1];
break;
default:
++letter[2];
}
printf("\nletter[0] contains: %d\nletter[1] contains:"
" %d\nletter[2] contains: %d", letter[0], letter[1], letter[2]);
fflush(stdin);
getchar();
return 0;
}
You really need a break in those cases or else all the cases will be performed.
edit: unless its your intention to add one to letter[1] and letter[2] when you enter a to add
one to letter[0]