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

/*  A program to count the number of grades occurring in the input */

#include <stdio.h>
#define DEBUG

int main()
{
  /* Declare variables */
  int a;
  int b;
  int c;
  int d;
  int f;
  int others;
  int ch; /* for input */
 
       /* 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 ((ch = getchar()) != EOF && ch != '\n')
       {

        /* Print all characters read */
        printf("%c", ch); /* I removed the '\n' to make it appear as you wanted */

              /* Count inputs */
              switch(ch)
              {
                 case 'a': 
                 case 'A':a++; break;
                 case 'b': 
                 case 'B':b++; break;
                 case 'c': 
                 case 'C': c++; break;
                 case 'd': 
                 case 'D':d++; break;
                 case 'f': 
                 case 'F': f++; break;
                 default: others++; break;
              }
       }
       putchar('\n'); /* add a new line to display the result in its own line */

       /* 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);

       return 0;
}

A few errors you had. Take a look at the red notations.
The while loop needs to have a way to stop that it is not only the EOF. If not, you can not see the result. '\n' seems adequate to me.
You need to break in the switch after every grade, or it will go through it no matter what. I used that technique to accommodate that 'a' and 'A' is the same grade.
main() returns an int. You were missing it.
Of course, ch variable must be different than c for the grade as was said before.

Edited 6 Years Ago by Aia: n/a

/*  A program to count the number of grades occurring in the input */

#include <stdio.h>
#define DEBUG

int main()
{
  /* Declare variables */
  int a;
  int b;
  int c;
  int d;
  int f;
  int others;
  int ch; /* for input */
 
       /* 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 ((ch = getchar()) != EOF && ch != '\n')
       {

        /* Print all characters read */
        printf("%c", ch); /* I removed the '\n' to make it appear as you wanted */

              /* Count inputs */
              switch(ch)
              {
                 case 'a': 
                 case 'A':a++; break;
                 case 'b': 
                 case 'B':b++; break;
                 case 'c': 
                 case 'C': c++; break;
                 case 'd': 
                 case 'D':d++; break;
                 case 'f': 
                 case 'F': f++; break;
                 default: others++; break;
              }
       }
       putchar('\n'); /* add a new line to display the result in its own line */

       /* 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);

       return 0;
}

A few errors you had. Take a look at the red notations.
The while loop needs to have a way to stop that it is not only the EOF. If not, you can not see the result. '\n' seems adequate to me.
You need to break in the switch after every grade, or it will go through it no matter what. I used that technique to accommodate that 'a' and 'A' is the same grade.
main() returns an int. You were missing it.
Of course, ch variable must be different than c for the grade as was said before.

Thank you so much for your help.
I greatly appreciate it!

You need to break in the switch after every grade, or it will go through it no matter what.

Aia got confused with your non-standard syntax as I almost did:

switch(ch)
              {
                 case 'a': 
                 case 'A':a++; break;
                 case 'b': 
                 case 'B':b++; break;

'Proper' (less confusing) syntax is:

switch(ch)
              {
                 case 'a': 
                 case 'A':
                         a++; 
                         break;
                 case 'b': 
                 case 'B':
                         b++; 
                         break;
This question has already been answered. Start a new discussion instead.