# Switch Case for Counting Frequency of numbers

• 07-30-2012
sjmp
Switch Case for Counting Frequency of numbers
The goal is to count the frequency of no.s(0,1,2,3,4) and increment all other characters.

Only the default case increments. I have a break statement which I thought would kick back to while which precedes the switch case statement.

I am still trying to figure out how to search for a number - do I look for 0 or '0'?

Thanks in advance for any help on pointing me in the right direction.

Code:

```#include <stdio.h> #include <cctype> int main() {     int c, nzero, none, ntwo, nthree, nfour, nother;     nzero = none = ntwo = nthree = nfour = nother = 0;         while ((c = getchar()) && c != EOF)     {         switch (c)         {         case 0:             if(c == 0 && isdigit(c))                 ++nzero;             break;         case 1:             if(c == 1 && isdigit(c))                 ++none;             break;         case 2:             if(c == 2 && isdigit(c))                 ++ntwo;             break;         case 3:             if(c == 3 && isdigit(c))                 ++nthree;             break;         case 4:             if(c == 4)                 ++nfour;             break;         default:             if(isalpha(c) || isdigit(c))                 ++nother;             if(c = EOF)                 break;         }     }         printf("Zero occurs %d times\nOne occurs %d times\nTwo occurs %d times\nThree occurs %d times\nFour occurs %d times\nOther Characters appear %d times\n", nzero,none,ntwo,nthree,nfour,nother);     return 0;```
• 07-30-2012
Matticus
"getchar()" will return an integer value that represents the code for the character entered. It looks like you want to check if that character is a numeric symbol (i.e. 1, 2, 3, etc). Therefore, you must compare it against that character (i.e. '1', '2', '3', etc) and not the integer value itself.

The integer values that correspond to each character can be found on a standard ASCII chart. Note that the character '1' has a decimal integer value of 49, etc.

I must ask - it seems you had an idea that this was the way to go - have you tried it in the code before asking for help?
• 07-30-2012
sjmp
Yes I tried - definitely confused on how the character/number is read by the computer. Also know the default as is will increment everything that falls under getchar(c) - and I need to fix that.

Code:

```case 0:         if(c == '0')               ++nzero;```
for Each but again the only thing that works is Other:
Code:

```default:                         if(isalpha(c) || isdigit(c))                                 ++nother;                         if(c = EOF)                                 break;```
• 07-30-2012
Matticus
Code:

```default:     if(isalpha(c) || isdigit(c))     ++nother;     if(c = EOF)  // <--- You have the wrong operator here, you want ==     break;```
Quote:

definitely confused on how the character/number is read by the computer
This is simple enough that if you're having trouble understanding it, do a little practice program and study the output.

Code:

```int userInput; userInput = getchar(); // assume user enters 1 printf("Character = %c\nInteger Value = %d\n", userInput,userInput); if(userInput == 1)     printf("Integer 1\n"); if(userInput == '1')     printf("Character 1\n");```
• 07-30-2012
std10093
Quote:

Originally Posted by sjmp
..that falls under getchar(c) ....

Why not using scanf ?Then you are going to read numbers and things will get much easier(i my opinion :) )
• 07-30-2012
sjmp
We have not gotten to scanf - but I will research it. I was orignally hoping that this would have done the trick

Code:

```int main() {         int c, nzero, none, ntwo, nthree, nfour, nother;         nzero = none = ntwo = nthree = nfour = nother = 0;                 while ((c = getchar()) && c != EOF)         {                 switch (c)                 {                 case 't':                 case '\n':                 case ' ':                         break;                 case '0':                         ++nzero;                         break;                 case '1':                         ++none;                         break;                 case '2':                         ++ntwo;                         break;                 case '3':                         ++nthree;                         break;                 case '4':                         ++nfour;                         break;                 default:                         ++nother;                         break;                 }         }                 printf("Zero occurs %d times\nOne occurs %d times\nTwo occurs %d times\nThree occurs %d times\nFour occurs %d times\nOther Characters appear %d times\n", nzero,none,ntwo,nthree,nfour,nother);         return 0; }```
• 07-30-2012
Matticus
This code is different. Is it not working for you?
• 07-30-2012
sjmp
actually this does do the trick.

Matticus - thank you - running the simple program to see the output - answers all questions. I need to work on troubleshooting. Been doing this for a few weeks now.

Thanks for the direction - SP
• 07-30-2012
std10093
I think that this code you posted works,isn't it?

TIp:Try to write int main(void) instead of int main() :)

EDIT->I am slower than a replay! :P
• 07-30-2012
Another way to do it:

Code:

```#include <stdio.h> #include <ctype.h>    int main(void) {   int c, i,nother=0;   int numbers[5]={0};       while ((c = getchar()) && c != EOF) {       if(isdigit(c)) {         c-='0';         numbers[c]++;       }       else if(isalpha(c))         nother++;   }   for(i=0;i<5;i++) {       printf("%d occurs %d times\n",i,numbers[i]);   }   printf("Other characters occurred %d times\n",nother);   return 0; }```
• 07-30-2012
sjmp
thanks that looks a lot cleaner - but we have to use a switch for the assignment.
• 07-30-2012
AndiPersti
```\$ cat test.c #include <stdio.h> #include <ctype.h>    int main(void) {   int c, i,nother=0;   int numbers[5]={0};   int x = 999;       while ((c = getchar()) && c != EOF) {       if(isdigit(c)) {         c-='0';         numbers[c]++;       }       else if(isalpha(c))         nother++;   }   for(i=0;i<5;i++) {       printf("%d occurs %d times\n",i,numbers[i]);   }   printf("Other characters occurred %d times\n",nother);   printf("x = %d\n", x);   return 0; } \$ gcc -o test test.c \$ ./test 555666777888999 0 occurs 0 times 1 occurs 0 times 2 occurs 0 times 3 occurs 0 times 4 occurs 0 times Other characters occurred 3 times    <-- WTF, I've just pressed CTRL-D (=EOF) after the numbers x = 1002      <-- And it looks like we've changed x too!!```