I am trying to create a program to read a series of input characters representing grades (e.g., an 'a' or 'A' represents an A, a 'b' or 'B' represents a 'B', and so on) and prints the counts of each grade.
/* A program to count the number of grades occuring in the input */
#include <stdio.h>
#define DEBUG
int main()
{
/* Declare variables */
int a;
int b;
int c;
int d;
int f;
int others;
/* Initialize variables at zero */
a = b = c = d = f = 0;
others = 0;
/* While c is a character and not equal to end of file */
while ((c = getchar()) != EOF)
{
/* Print all characters read */
printf("%c\n", c);
/* Count inputs */
switch(c)
{
case 'a': a++;
printf("a = %d\n", a);
case 'b': b++;
printf("b = %d\n", b);
case 'c': c++;
printf("c = %d\n", c);
case 'd': d++;
printf("d = %d\n", d);
case 'f': f++;
printf("f = %d\n", f);
default: others++;
printf("others = %d\n", others);
}
}
/* Print results */
printf("Grade counts:\n");
printf(" A's: %d\n", a);
printf(" B's: %d\n", b);
printf(" C's: %d\n", c);
printf(" D's: %d\n", d);
printf(" F's: %d\n", f);
printf(" Other grades: %d\n", others);
}
The program compiles, but I am not getting any right input. The counts are not correct. Did I code it incorrectly?
I am hoping to get an output like this:
abcDFEGH
DdFFEEaA
Grade counts:
A's: 3
B's: 1
C's: 1
D's: 3
F's: 3
Other grades: 5