2

Trying to make a GPA calculator. Here is my code:

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main()
{
    float fgpa, grade, n;
    char reply;
    
    n     = 1;
    grade = 1;
    
    printf("--- GPA Calculator ---");
    while(fgpa < 1 || fgpa > 4)
    {
        printf("\n\nEnter your current GPA: ");
        scanf("%f", &fgpa);
        if(fgpa < 1 || fgpa > 4)
            printf("Invalid Input! Please re-enter value.");
    }
    
    printf("\nUsing the following table to convert grade to points\n\nA+ = 4.0\nA = 4.0\nB+ = 3.5\nB = 3\nC+ = 2.5\nC = 2.0\nD+ = 1.5\nD = 1\nF = 0\n");
    
    while(grade > 0, n++)
    {
        printf("\nEnter points for module %.0f, enter \"0\" if there are no more additional modules: ", n);
        scanf("%f", &grade);
        printf("%f", grade);
        fgpa = (fgpa + grade) / n;
    }
        fgpa = fgpa* n / (n-1);
        n--;
        
    printf("\n\nNumber of modules taken: %.0f\nFinal GPA: %.1f", n, fgpa);
    
    return 0;
}

I've tried using if(grade = 0) break; but its still not breaking the loop even when the grade is correctly read 0.

picture of 0 being read correctly but loop still continuing

rewed
  • 61
  • 1
  • `fgpa` is never initialized. Imagine what happens if the initial value of `fgpa` is 2. There are most likely more problems. – Jabberwocky May 21 '22 at 10:07
  • `while(grade > 0, n++)` -- you use a comma operator here, which evaluates to the right-hand part, `n++`. `n` is 1 and you only ever increase it, so the overall condition is always false. You probably want `for(n = 1; grade > 0; n++)` – M Oehm May 21 '22 at 10:08
  • 1
    `while(grade > 0, n++)` does not what you think it does. Read this: https://stackoverflow.com/questions/52550/what-does-the-comma-operator-do – Jabberwocky May 21 '22 at 10:08
  • On the other hand, `if(grade = 0)` is not a comparison for zero; it assigns the value tero to `grade`, The condition id the value of the assignment, zero, which means false. You want `grade == 0` here. – M Oehm May 21 '22 at 10:11
  • These are frequent pitfalls for beginners in C. The compiler will warn you about many of them if you activate warnings, usually with `-Wall`. – M Oehm May 21 '22 at 10:12

1 Answers1

1

There are multiple problems in the code:

  • fgpa is uninitialized so the first test in the loop has undefined behavior.

  • you should also test the return value of scanf() to detect invalid or missing input.

  • while (grade > 0, n++) is incorrect too: you should instead always read the next grade and test its value and break from the loop before incrementing n.

  • Your averaging method seems incorrect too: you do not give the same weight to every module.

It seems more appropriate for your purpose to use for ever loops (for (;;)), unconditionally read input, check for scanf() success and test the input values explicitly before proceeding with the computations.

Here is a modified version:

#include <stdio.h>

// flush the rest of the input line, return EOF at end of file
int flush(void) {
    int c;
    while ((c = getchar()) != EOF && c != \n')
        continue;
    return c;
}

int main() {
    float fgpa;
    float grade;
    int n = 1;
    char reply;
    
    printf("--- GPA Calculator ---");
    for (;;) {
        printf("\n\nEnter your current GPA: ");
        if (scanf("%f", &fgpa) == 1) {
            if (fgpa >= 1 && fgpa <= 4)
                break;
            }
        } else {
            if (flush() == EOF) {
                fprintf(stderr, "unexpected end of file\n");
                return 1;
            }
        }
        printf("Invalid Input! Please re-enter value.\n");
    }
    
    printf("\nUsing the following table to convert grade to points\n\n"
           "A+ = 4.0\nA = 4.0\nB+ = 3.5\nB = 3\nC+ = 2.5\n"
           "C = 2.0\nD+ = 1.5\nD = 1\nF = 0\n");
    
    for (;;) {
        printf("\nEnter points for module %d, enter \"0\" if there are no more additional modules: ", n);
        if (scanf("%f", &grade) == 1) {
            if (grade <= 0)
                break;
            printf("%f\n", grade);
            fgpa = fgpa + grade;
            n = n + 1;
        } else {
            if (flush() == EOF)
                break;
            printf("Invalid Input! Please re-enter value.\n");
        }
    }
    fgpa = fgpa / n;
        
    printf("\n\nNumber of modules taken: %d\nFinal GPA: %.3f\n", n, fgpa);
    
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189