# Thread: Quick help needed on simple example

1. ## Quick help needed on simple example

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?

2. Hello.

Line 2 says: "letter is an array of 3 integers.". There is nothing more to it.

3. Is this even being used in the code though. I can see letter[0], letter[1] and letter[2], but no mention of just letter.

Sorry if im being dumb.

4. letter[0] is the first element of the array declared on line 2. letter[1] is the second element of the array, etc.

--
Mats

5. Ah i understand now, thank you.

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

7. do not fflush stdin - read FAQ

Popular pages Recent additions