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

Recommended Answers

All 4 Replies

Other than c being use to accept input and count the number of 'c's, I seen nothing wrong.

/*  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.

/*  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;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.