MIKEY
MIKEY

Reputation: 1

Cumulative Grade Point Average Calculator written C

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

This is my code so far. I also wanted to include a way to check to make sure the user doesn't enter a character (a-z) in the function number_subjects but wasn't sure of the proper way to do that. I also believe something is wrong with my gpa calculation formula at the end of the main function. I've also added a do while statement to prompt the user if they want to try again at the end of the program but for some reason the program always restarts without taking an input form the user.

#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){

/* Quits Program */

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

/* Function call for number of subjects */

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

/* Asks user to name and input 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]);

/* Conversion for grades */

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 to calulate GPA */

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 course information back to 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 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 inputs 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);
}

Upvotes: 0

Views: 5750

Answers (1)

Mihai Maruseac
Mihai Maruseac

Reputation: 21460

The name of the array is a pointer in itself (the formal phrase is the name of the array decays into a pointer to the first element of the array). So you don't use & in there:

Instead of

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

you should have

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

Edit: Just saw you have two errors, not one. The second one is because your grade_point is a single value instead of a vector. Declare it as double grade_point[10] (see the [10] part).

Upvotes: 1

Related Questions