mecapet
mecapet

Reputation: 9

Grade calculation problems

Program will read data from a file which includes student names and grades, and will prompt user to enter a student's name for grade information. Then the program will calculate said grades with respective scores and assignment percentages. Then this data will be used to calculate a final numerical grade. The program will then assign a letter grade based on the final numerical grade. Descriptive information will output regarding the student's grades.

The instruction is to abide by these stipulations:

I am running into issue with the grade values computing incorrectly (though we are given a specific formula to use verbatim), and gaining general understanding of what is wanted overall.

This is the code I have thus far:

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main() {
    int totalGrades; // This is the total number of test grades
    int grade; // This is the student's test grade
    ifstream inFile; // This is a declaration of the inFile that holds all the grades
    string fileName; // This is the filename that the user will enter
    string name; // This is the name of the student
    string surname;
    int gradeCount; // This is to keep count of the number of grades entered
    int hwGradesN; // Number of homework grades
    int hwGradesV; // Homework grade values
    float hwGradesP; // Homework grade percentage
    int progGradesN; // Number of program grades
    int progGradesV; // Program grade values
    float progGradesP; // Program grade percentage
    int examGradesN; // Number of exam grades
    int examGradesV; // Exam grade values
    float examGradesP; // Exam grade percentage
    int totalHWpoints; // Total homework points earned
    int totalProgpoints; // Total program points earned
    int totalExampoints; // Total exam points earned
    int maxHWpoints; // The maximum points available for homework grades
    int maxProgpoints; // The maximum points available for program grades
    int maxExampoints; // The maximum points available for exam grades
    int maxCoursepoints; // The maximum points available for the course
    float finalGrade; // Final grade
    float finalGradeLetter;     // Letter grade assigned from numerical grade
    float average; // The average of all the grades

    cout << "Enter the input file name to read student grades: ";
    cin >> fileName;

    // Open the file with the grades
    inFile.open(fileName.c_str ());

    // Check to make sure the file opened correctly
    if(!inFile) {
        cout << "File did not open correctly." << endl;
        return 1;
    }

    // Reads scores from file
    while(!inFile.eof()) {

        totalHWpoints = 0;
        totalProgpoints = 0;
        totalExampoints = 0;
        inFile >> name;
        inFile >> surname;
        inFile >> hwGradesN;

        maxHWpoints = hwGradesN * 100;

        while(hwGradesN--) {
            inFile >> hwGradesV;
            totalHWpoints += hwGradesV;
        }

        inFile >> hwGradesP;
        maxProgpoints = hwGradesP * 100;
        inFile >> progGradesN;

        while(progGradesN--) {
            inFile >> progGradesV;
            totalProgpoints+=progGradesV;
        }

        inFile >> progGradesP;
        inFile >> examGradesN;
        maxExampoints = examGradesN * 100;

        while(examGradesN--) {
            inFile >> examGradesV;
            totalExampoints += examGradesV;
        }

        inFile >> examGradesP;
        maxCoursepoints = (maxExampoints + maxHWpoints + maxProgpoints);
        finalGrade = (((totalHWpoints/maxHWpoints) * hwGradesP)
                   + ((totalProgpoints/maxProgpoints) * progGradesP)
                   + ((totalExampoints/maxExampoints) * examGradesP))
                   * 100;

        // Calculate the average.
        average = (finalGrade) / (maxCoursepoints);
        cout << average << endl; // Display the average.

        if(average >= 90) finalGradeLetter = 'A';
        if(average < 90 && average >= 80 ) finalGradeLetter = 'B';
        if(average < 80 && average >= 70) finalGradeLetter = 'C';
        if(average < 70 && average >= 60) finalGradeLetter = 'D';
        if(average <= 60) grade = 'F'; finalGradeLetter = 'F';

        cout << "Final Grade of "<< name <<" "<< surname <<": " << finalGradeLetter << endl;

    }

    return 0;
}

Output reads:

Enter the input file name to read student grades: studentGrades.txt
1.06504
Final Grade of Sherlock Holmes: 70
1.04065
Final Grade of Constant Success: 70

Process returned -1073741676 (0xC0000094)   execution time : 8.937 s
Press any key to continue.

((This output does not show correct calculations, and does not read the entire file, which is a new issue and I'm not sure what caused these issues))

Upvotes: 0

Views: 241

Answers (1)

Christopher Townsend
Christopher Townsend

Reputation: 1745

You will have to check for 0's and handle them appropriately.

        ...

        maxCoursepoints = (maxExampoints + maxHWpoints + maxProgpoints);

        if (maxHWpoints == 0 || maxProgpoints == 0 || maxExampoints == 0 || maxCoursepoints == 0)
        {
            cout << "Unable to calculate grade";

            //lookup grade here instead
        }
        else 
        {

            finalGrade = (((totalHWpoints / maxHWpoints) * hwGradesP) +
                ((totalProgpoints / maxProgpoints) * progGradesP) +
                ((totalExampoints / maxExampoints) * examGradesP)) * 100;

            // Calculate the average.
            average = (finalGrade) / (maxCoursepoints);
        }

        cout << average << endl; // Display the average.

        ...

This might also be a useful read : https://www.geeksforgeeks.org/handling-the-divide-by-zero-exception-in-c/

Upvotes: 1

Related Questions