-1

I am trying to design this program and this always gives me wrong grades entered error can anyone help me find out where is the error? Can anyone see what mistakes I have made and possibly push me in the right direction? I appreciate anyone's time and advice :)

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
  int main() {
        char str[100], ch;
        int i, grade[20];
        int p;
        float credit[20], gpa = 0.0, totCredit = 0.0;

        printf("SIKKIM MANIPAL INSTITUTE OF TECHNOLOGY\nMajhitar, Rangpo - 737136\n\nGPA CALCULATOR \n\n");
        printf("PROGRAM CREATED BY: SHAMSH SAMEED ASHAN\n");
        printf("FOR ANY INFO PLEASE CONTACT +91-9083454677\n\n");
        printf("------*------*------*-----*------*------*------*------*------*------*------*------*------*------\n\n");
        printf("NOTE:\n\n");
        printf("1. Enter Grades in CAPITALS \n");
        printf("2. Enter Grade and credit for the subjects in the following format:\n");
        printf("   Example: |Subject 'n' (Grade|Credit):S4 |(If you enter S4 Here S is the grade and 4 is the credit)\n");
        printf("3. If you have got 'F' grade then enter F followed by credit for that subject. \n   'F' grade subjects will not be used in calculation.\n\n");
        printf("Enter Grade & Credits for each subject:\n\n");
        printf("------*------*------*-----*------*------*------*------*------*------*------*------*------*------\n\n");
        printf("Enter number of subject:");
        scanf("%d",&p);
        /* get the letter grade and credits from the user */
        for (i = 0; i < p; i++) {
                printf("Subject %d(Grade|Credit):", i + 1);
                ch = getchar();
                grade[i] = ch;
                scanf("%f", &credit[i]);
                getchar();
        }

        /* print the input grades and credits */
        printf("\nSubject | Grade | Credit\n");
        for (i = 0; i < p; i++) {
                printf("   %d    |  %c    | %.1f\n", i + 1, grade[i], credit[i]);
        }

        /* calculate gpa value */
        for (i = 0; i < p; i++) {
                switch (grade[i]) {
                        case 'S':
                                gpa = gpa + 10 * credit[i];
                                totCredit = totCredit + credit[i];
                                break;
                        case 'A':
                                gpa = gpa + 9 * credit[i];
                                totCredit = totCredit + credit[i];
                                break;

                        case 'B':
                                gpa = gpa + 8 * credit[i];
                                totCredit = totCredit + credit[i];
                                break;

                        case 'C':
                                gpa = gpa + 7 * credit[i];
                                totCredit = totCredit + credit[i];
                                break;

                        case 'D':
                                gpa = gpa + 6 * credit[i];
                                totCredit = totCredit + credit[i];
                                break;

                        case 'E':
                                gpa = gpa + 5 * credit[i];
                                totCredit = totCredit + credit[i];
                                break;

                        case 'F':
                                gpa = gpa + 0 * credit[i];
                                totCredit = totCredit + credit[i];
                                printf("\nNOTE: F Grade SUBJECTS will not be used in calculation.\n");
                                break;

                        default:
                                printf("WRONG GRADES FORMAT ENTERED\n PLEASE ENTER GRADES IN CAPITAL LETTERS!!\n\n");
                                exit(0);

                }
        }
        printf("Total GPA: %.1f\tTotal Credit: %.1f\n", gpa, totCredit);
        gpa = gpa / totCredit;
        printf("Your GPA: %.2f\n", gpa);

        printf("\nPress ENTER a few times to terminate the program");
        fflush(stdout);
        getchar(); getchar();
        return 0;
  }

1 Answers1

0
ch = getchar();

This gets the next char from STDIN. Unless you enter the letter straight after the number (for example by entering 2A when prompted for a subject) this will be a \n.

To skip whitespace you can read the character like this:

scanf(" %c", &ch);

The initial space will make it skip whitespace and read the first non-whitespace character.

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82