Cumulative Grade Point Average Calculator Written in C

I am working on a GPA calculator and I am new to C. I am writing this code using gvim in Ubuntu 12 and compiling with gcc in the terminal.

This is my code. I also wanted to include a validation method to make sure the user was not typing the (az) character into the number_subjects function, but wasn't sure about the correct way to do this. I also believe there is something wrong with my gpa calculation formula at the end of the main function. I also added a do while statement to invite the user if they want to try again at the end of the program, but for some reason the program always reloads without accepting user input.

#include<stdio.h>
#include<string.h>

int number_subjects(int num_sub);

int main(void) {

int class_grades[10];
int i;
int credit_hrs[10];
int num_sub;
int totalCreditHour = 0;
double sum_gpaxcredit_hrs = 0;
double grade_point[10];
double gpa;
char subject[10][10];
char grade[10][10];
char option, y_n;

do{

      

/* Main menu */

printf("\t*** GPA CALCULATOR ***\n\n");
printf("Please choose and option\n");
printf("[a] Calculate GPA\n[q] Quit\n");
scanf("%c", &option);

switch(option){

      

/ * Exit the program * /

case 'q':
return(0);
break;

      

/ * Call the function for the number of objects * /

case 'a':
num_sub = number_subjects(num_sub);         
break;
default:
printf("Not a valid choice\n");
break;
}

      

/ * Asks the user to name and enter grades for each class * /

for(i = 0; i <= num_sub -1; i++) {  
    printf("\n Class %d \n", i +1);
    printf("\nClass name : ");
    scanf("%s", subject[i]);
    printf("Enter grade: ");
    scanf("%d", &class_grades[i]);
    printf("Enter credit hours: ");
    scanf("%d", &credit_hrs[i]);

      

/ * Convert for ratings * /

if(class_grades[i] >= 95 && class_grades[i] <=100)
    {
        grade_point[i] = 4.00;
        strcpy(grade[i], "A+");
    }
    else if(class_grades[i] >= 90 && class_grades[i] <=94)
    {
        grade_point[i] = 4.00;
        strcpy(grade[i], "A");
    }
    else if(class_grades[i] >= 85 && class_grades[i] <= 89)
    {
        grade_point[i] = 3.33;
        strcpy(grade[i], "B+");
    }
    else if(class_grades[i] >= 80 && class_grades[i] <= 84)
    {
        grade_point[i] = 3.00;
        strcpy(grade[i], "B");
    }
    else if(class_grades[i] >= 75 && class_grades[i] <= 79)
    {
        grade_point[i] = 2.33; 
        strcpy(grade[i], "C+");     
    }
    else if(class_grades[i] >= 70 && class_grades[i] <= 74)
    {   
        grade_point[i] = 2.00;
        strcpy(grade[i], "C");
    }   
    else if(class_grades[i] >= 60 && class_grades[i] <= 69)
    {
        grade_point[i] = 1.00;
        strcpy(grade[i], "D");
    }
    else if(class_grades[i] >= 0 && class_grades[i] <= 59)
    {   
        grade_point[i] = 0.0;
        strcpy(grade[i], "F");
    }
}

      

/ * Formula for GPA calibration * /

for(i = 0; i <= num_sub -1; i++) {

    sum_gpaxcredit_hrs = grade_point[i] * credit_hrs[i];
    gpa = sum_gpaxcredit_hrs / credit_hrs[i];
} 

      

/ * Displays all information about the course back to the user * /

for(i = 0; i <= num_sub -1; i++) {

    printf("\n%d\t%s\t\t%d\t %.2f\t\t%s\t\n", i +1, subject[i],class_grades[i], grade_point[i], grade[i]);
}

      

/ * Prints out the GPA * /

printf("\n\n GPA is %.2f\n\n\n", gpa);

printf("Would you like to try again?\n");
printf("[y] yes\n[n] no\n");
scanf("%c", &y_n);
}while(y_n ='n');
    printf("Goodbye!\n");

return(0);

}

      

/ * User enters number of classes * /

int number_subjects(int num_sub){   

do {
    printf("Please enter the number of classes you are taking [Max 10] \n");        
    scanf("%d", &num_sub);

        if((num_sub >10) || (num_sub < 1))
            printf("**Please enter number between 1 and 10**\n");

}while((num_sub >10) || (num_sub <1));

return(num_sub);
}

      

0


source to share


1 answer


An array name is a pointer by itself (the formal phrase is an array name that decays to a pointer to the first element of the array). So you don't use &

there:

Instead

    scanf("%s", &subject[i]);

      



you must have

    scanf("%s", subject[i]);

      

Edit . I just saw that you have two errors, not one. The second is because yours grade_point

is the only value instead of a vector. Declare it as double grade_point[10]

(see section [10]

).

+1


source







All Articles