C++ - Student Grades

Modify student overall grades.

Header File

The header contains one class and one structure.

#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

const int SIZE = 25;

struct StudentRec{
    string name;
    int assignTotal, testTotal;
    char grade;
};

class ClassRoll{
    public:
        ClassRoll(string /*filename*/, string /*course name*/, int = 25/*max students*/);
        int searchStudent(string /*student*/)const;
        void showStudentGrade(string /*student*/)const;
        void writeStudents(ostream& /*filename*/);
        int getNoStudents()const{return noStudents;}
        string getCourseName()const{return courseName;}
        void changeGrade(string /*student*/, char /*grade*/);
    private:
        StudentRec stud[SIZE];
        string courseName;
        int maxStudents, noStudents;
};

Default Constructor

Checks to see if the course file is already opened or not and then inputs the data (student's grades) from that saved file. Converts grades to letters only on the client side.

ClassRoll::ClassRoll(string newFile, string newCourse, int newMaxStudents){
    courseName = newCourse;
    maxStudents = newMaxStudents;
    noStudents = 0;
    ifstream fin(newFile);
    if(!fin.is_open()){
        cout << "Could not open file. " << endl;
        system("pause");
        exit(666);
    }

    for(int ctr=0; ctr < maxStudents; ctr++){
        getline(fin,stud[ctr].name);
        fin >> stud[ctr].assignTotal;
        fin >> stud[ctr].testTotal;
        if(stud[ctr].testTotal >= 270)
            stud[ctr].grade = 'A';
        else if(stud[ctr].testTotal >= 240)
            stud[ctr].grade = 'B';
        else if(stud[ctr].testTotal >= 210)
            stud[ctr].grade = 'C';
        else if(stud[ctr].testTotal >= 180)
            stud[ctr].grade = 'D';
        else
            stud[ctr].grade = 'F';
        fin.clear();
        fin.ignore(80,'\n');
        if(fin.eof()){
            break;
            exit(666);
        }
        noStudents ++;
    }
}

Constructors

Show a specific student's grade.

void ClassRoll::showStudentGrade(string showStudent)const{
    bool notFound = true;
    for(int ctr=0; ctr < noStudents; ctr++){
        if(showStudent == stud[ctr].name){
            cout << showStudent << " grade is: " << stud[ctr].grade << endl << endl;
            notFound = false;
        }
    }
    if(notFound == true)
        cout << "Student not found." << endl << endl;
}

Changes the grade of a specific student.

void ClassRoll::changeGrade(string newStudent, char newGrade){
    bool notFound = true;

    for(int ctr=0; ctr < noStudents; ctr++){
        char oldGrade = stud[ctr].grade;
        newGrade = toupper(newGrade);
        if(newStudent == stud[ctr].name)
            if(newGrade == 'A' || newGrade == 'B' || newGrade == 'C' || newGrade =='D' || newGrade == 'F' && newGrade == oldGrade){
                notFound = false;
                stud[ctr].grade = toupper(newGrade);
                cout << stud[ctr].name << "'s grade has changed from " << oldGrade << " to " << newGrade << "." << endl << endl;
            }
            else
                cout << stud[ctr].name << "'s grade cannot be changed because the grade assigned matches the current grade or you must choose A,B,C,D, or F." << endl << endl;
    }

    if(notFound == true)
        cout << "Student not found." << endl << endl;
}

Writes the modified grades back into the file.

void ClassRoll::writeStudents(ostream&output){
    ofstream fout("output.rpt");
    if(!fout.is_open()){
        cout << "Could not open file. " << endl;
        system("pause");
        exit(666);
    }

    fout << left << setw(14) << "Student Name" 
           << setw(14) << "Assign Total" 
           << setw(14) << "Test Total" 
           << setw(14) << "Grade" 
           << endl << endl;

    for(int ctr = 0; ctr < noStudents; ctr++){
        fout << left << setw(13) << stud[ctr].name
             << right << setw(13) << stud[ctr].assignTotal 
             << setw(12) << stud[ctr].testTotal
             << setw(9) << stud[ctr].grade << endl;
    }
    system("type output.rpt");
}

Client Code

#include <string>
#include <iostream>
#include "classroll.h"
using namespace std;

void showCourse(ClassRoll);

int main(){
    cout << "comp 333 object examples" << endl<< endl;
    ClassRoll comp333("student.dat","OOP C++",3);
    cout << "Number of students in COMP 333 = " << comp333.getNoStudents()<< endl;
    comp333.writeStudents(cout);
    comp333.showStudentGrade("J. Doe");
    comp333.changeGrade("T. Jones", 'B');
    showCourse(comp333);
    ofstream fout("sample.dat");
    if (!fout.is_open()){
        cout << "error opening file "<<endl;
        system("pause");
        exit(1);
    }
    comp333.writeStudents(fout);
    system("type sample.dat");
    system("pause");
}

void showCourse(ClassRoll comp333){
    cout << "Course Name: " << comp333.getCourseName() << endl;
    cout << "Number of Students: " << comp333.getNoStudents() << endl << endl;
}

More function calls would look like this inside the main().

cout << "\n\nourClass object examples" << endl << endl;
ClassRoll ourClass("nofile.dat","c++");
cout << "Number of students in ourClass = " << ourClass.getNoStudents()<< endl << endl;
ourClass.changeGrade("T. Jones", 'B');
ourClass.writeStudents(cout);
ourClass.showStudentGrade("J. Doe");

cout << "\n\nbestClass object examples" << endl<<endl;
ClassRoll bestClass("myfile.dat","poker",500);
bestClass.writeStudents(cout);
cout << "Number of students in ourClass = " << bestClass.getNoStudents()<< endl << endl;
bestClass.writeStudents(cout);
bestClass.showStudentGrade("J. Doe");

Input Data/File

J. Doe
50  290
P. Smith
45  190
T. Jones
50   100

Output File

Student Name  Assign Total  Test Total    Grade         

J. Doe                  50         290        A
P. Smith                45         190        D
T. Jones                50         100        B