0

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:

  • Create an array of the structure in main to hold 10 components, i.e., an array of size 10.
  • Using a function, open the input file, the output file for report and an error file to log errors. If any file fails to open, display a proper message to the customer and exit the program. Request the input and report file names from the user. The error file name may be a literal. Call the function from main.
  • Using a function, close all files prior to program end. Call this function from main.
  • Read the file and process the data via a function. This function will call the remaining functions EXCEPT the output/display function. Read until end of file. Do not “hard-code” the number of rows to read. Read the first name and last name into the student array data members. Since the array may hold more components than there is data, use and increment a counter to count how many rows “actually” read. Pass that counter into the output function.
  • The other data for the student should be read into non-structure variables.
  • There will be errors in the file, i.e., divide by zero. If a divide by zero error is encountered, do not calculate the numerical grade, lookup the final letter grade, lookup the comment or output the student grade information. Write an error to the error file indicating that the grade cannot be calculated for the student. Include the student name. If there is an error, set the student’s numerical grade to 0, letter grade to space (‘ ‘) and comment to “Error in Data”.
  • Calculate numerical grade. Store the numerical grade in the student array data member. You may call this function from the read and process function.
  • Look up the final letter grade using a function. Store the letter grade in the student array data member. You may call this function from the read and process function.
  • Retrieve the comment via a function. Store the comment in the student array data member. You may call this function from the read and process function.
  • Using a function, for each student, output the following to the console (screen) and the output file in a neat, readable format. Output each input line on one output line. Use manipulators to output values in readable columns, set any floating-point numbers to 2 decimal places. Call this function from main.

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

Casey
  • 8,686
  • 9
  • 52
  • 79
mecapet
  • 9
  • 1
  • 3
    Googling for the error code: Integer Divide by Zero Exception (0xC0000094) – JVApen Nov 11 '19 at 18:14
  • `while(!inFile.eof()){` [https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – drescherjm Nov 11 '19 at 18:18
  • Please reformat your code. – nicomp Nov 11 '19 at 18:24

1 Answers1

1

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/