Creating a GPA Calculator in C Code

I just wrote this code and I can't seem to get it to work - let alone output the results I want. I have to create a program that will compute the GPA of an unknown number of students (which I narrowed down to a maximum of 25) with an unknown number of classes respectively (also narrowed down to 10 max, to make my life easier).

Can anyone see what mistakes I've made and maybe push me in the right direction? I appreciate any time and advice :)

My code:

// C Program - GPA calculator for x amount of students with x amount of classes
// Program developer: Diana Wright - Novembet 22nd, 2014
#include <stdio.h>

int main(void) {

    // Declare variables
    int grade[5], g, class_num[9], c;
    char name[24], n;
    float GPA=0.0, grade_point=0.0, result=0.0;

    // Input student names implying amount of students
    for(n=0; n<25; n++) {
        scanf(" %s", &name[n]);
        printf("Please enter student names: %s.", name[n]);

        // Input number of classes
        for(c=0; c<10; c++) {
            scanf(" %d", &class_num[c]);
            printf("Please enter amount of classes (max.10): %d", class_num[c]);

            // Input grades
            for(g=0; g<=class_num; g++) {
                scanf(" %d", &grade[g]);    
                printf("Please enter students' grades: %d.", grade[g]);
            }

            // Conversion of grades
            if (grade == "A"){
                grade_point = 4.0;
            }
            if (grade == "B"){
                grade_point = 3.0;
            }
            if (grade == "C")
            {
                grade_point =2.0;
            }
            if (grade == "D"){
                grade_point = 1.0;
            }
            if (grade == "F"){
                grade_point = 0.0;
            }

            // Formula to calculate GPA
            result += grade_point[g];
            GPA = result/class_num[c];
        }
    }

    // Print user input and results in a table
    printf("\n Student name \t Grades \t GPA");
    for(n=0; n<25; n++) {
        printf(" %s \t %d \t %f", name[n], grade[g], GPA);
    }
    return 0;
}

      

My initial login:

Diana 3 A A A Edward 4 B C B A Stuart 4 A F C D Bert 2 F F

      

+3


source to share


3 answers


A bit rusty on this stuff, but I think there are a few strange things here that might not have been intended.

for(g=0; g<=class_num; g++) 

      

We think we are looking for the input value, which is stored at offset c, not an array.

for(c=0; c<10; c++)

      

I'm not entirely sure about the meaning of this loop. We only need to get the number of classes once per student.



if (grade == "A")

      

Again, this did not capture the right offset in the array. Are single quotes needed to compare against char?

You will also have your own print instructions, printing the heap ... for example, you can move this line outside of the loop:

printf("Please enter students' grades: %d.", grade[g]);

      

+1


source


So don't dwell on your business about anything, be it homework or don't do your homework, and give credit to what the laughter said and the Gopi already said ...

There are some glaring bugs in the code that will prevent you from compiling and running any effect. First, let's consider declaring variables:

// Declare variables
// You don't initialize any of your char or int non array and array variables. 
int grade[5], g, class_num[9], c;
char name[24], n;
float GPA=0.0, grade_point=0.0, result=0.0; // You initialize your floating point variables.

      

When you initialize, the compiler will designate the memory region that these variables will reside in. If you don't, the compiler will start making "assumptions" for the variables you use and assign random memory locations to your variables. Next:

for (c = 0; c<10; c++) {
        scanf(" %d", &class_num[c]); // You ask the user to enter one digit at a time for the class number
        printf("Please enter amount of classes (max.10): %d", class_num[c]); 

        // Input grades
        for (g = 0; g <= class_num; g++) // You try and make a comparison to a single variable int to an entire array which is not allowed.
        {
            scanf(" %d", &grade[g]);
            printf("Please enter students' grades: %d.", grade[g]);
        }

      

You cannot compare one variable to the whole array, this string for (g = 0; g <= class_num; g++)

will not compile. Other:



if (grade == "A"){
        grade_point = 4.0;
        }

      

You are trying to do a sort comparison, which is the int datatype in your case, to a string / character literal "A"

, which is not allowed ...

result += grade_point[g]

      

Grade_point becomes an array here when it wasn't defined this way before ...

It looks like you rushed through the process of developing your program. I recommend that you sit down with a piece of paper and a pencil / pen and write down exactly what you are trying to accomplish at each step of the user's input request. This will help determine what types of variable data you should use to accomplish your tasks.

+1


source


// the following implements the needed data set,
// corrects several coding errors
// has (not yet run) compiling code

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

struct studentGrade
{
    char name[24];
    char grades[10];
    int  class_num;
    float GPA;
};


int main(void)
{

    // Declare variables
    struct studentGrade studentGrades[25] = {{"",{'F','F','F','F','F','F','F','F','F','F'},0,0.0F}};


    int  c; // index into student grades[]

    int n; // student number/loop counter


    float grade_point=0.0f, result=0.0f;

    // Input student names implying amount of students
    for(n=0; n< (sizeof( studentGrades)/sizeof(struct studentGrade)); n++)
    {
        result = 0.0f; // initialize for each student

        printf( "please enter student name:\n     ");
        scanf(" %s", (char*)(studentGrades[n].name) );
        printf("Student Name is: %s.", studentGrades[n].name);

        printf("please enter student class count:\n");
        scanf(" %d", &studentGrades[n].class_num);
        printf("Student Number of classes: %d", studentGrades[n].class_num);

        // Input class grades
        for(c=0; c< 10; c++)
        {
            printf("please enter grade for class: %d", c);
            scanf(" %c", &(studentGrades[n].grades[c]));
            printf("student grade for class: %d is %c\n", c, studentGrades[n].grades[c]);


            // following makes wild assumption that grade entered
            // is 'A'...'F'
            // Conversion of grades
            grade_point = 0.0f; // init for each class
            switch( studentGrades[n].grades[c] )
            {
                case 'A':
                    grade_point = 4.0f;
                    break;

                case 'B':
                    grade_point = 3.0f;
                    break;

                case 'C':
                    grade_point = 2.0f;
                    break;

                case 'D':
                    grade_point = 1.0f;
                    break;

                case 'F':
                    grade_point = 0.0f;
                    break;

                default:
                    printf( "invalid grade entered\n");
                    c--; // so will properly handle loop control, etc
                    break;
            }  // end switch
            result += grade_point;


            // Formula to calculate GPA
            result += grade_point;
        } // end for each grade

        studentGrades[n].GPA = result/(float)c;
    } // end for each student

    // Print user input and results in a table
    printf("\n Student name \tGPS\n\t\tgrades\n");

    for(n=0; n< (sizeof(studentGrades) / sizeof( struct studentGrade)); n++)
    {
        if( 0 != strlen(studentGrades[n].name) )
        {
            printf( " %s \tGPS: %.2f\n",
                studentGrades[n].name,
                studentGrades[n].GPA);
            for( c=0; c< studentGrades[n].class_num; c++)
            {
                printf("\tclass: %d\t%c:\n", c, studentGrades[n].grades[c] );
            }
        }
    }
    return 0;
}

      

+1


source







All Articles