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;