C++ Task You will design a set of classes for storing student information, along
ID: 3916786 • Letter: C
Question
C++
Task
You will design a set of classes for storing student information, along with a class that will manage a list of students. Data can be imported from files for storage in the list, and summary reports with computed final grades will be printed to output files.
Details
1. Design a set of classes that store student grade information. There should be one base class to store common data, and three derived classes that divide the set of students into three categories: English students, History students, and Math students. All data stored in these classes should be private or protected. Any access to class data from outside should be done through public member functions. The base class should allocate storage for the following data (and only this data):
~ student's first name (you may assume 20 characters or less)
~ student's last name (you may assume 20 characters or less)
~ Which course the student is in (English, History, or Math)
2. Each class should have a function that will compute and return the student's final average, based on the stored grades. All grades are based on a 100 point scale. Here are the grades that need storing for each subject, along with the breakdown for computing each final grade:
English -- Attendance = 10%, Project = 30%, Midterm = 30%, Final Exam = 30%
History -- Term Paper = 25%, Midterm = 35%, Final Exam = 40%
Math -- There are 5 quizzes, to be averaged into one Quiz Average (which can be a decimal number). Final grade computed as follows:
* Quiz Average = 15%, Test 1 = 25%, Test 2 = 25%, Final Exam = 35%
3. Write a class called StudentList, which will be used to store the list of various students, using a single array of flexible size. Note that this array will act as a heterogeneous list, and it will need to be dynamically managed. Each item in the array should be a Student pointer, which should point to the appropriate type of object. Your list should only be big enough to store the students currently in it. Your StudentList class needs to have these public functions available:
- StudentList() -- default constructor. Sets up an empty list
- ~StudentList() -- destructor. Needs to clean up all dynamically managed data being managed inside a StudentList object, before it is deallocated
- bool ImportFile(const char* filename)
~ This function should add all data from the given file (the parameter) into the internally managed list of students. The file format is specified below in this writeup. (Note that if there is already data in the list from a previous import, this call should add MORE data to the list). Records should be added to the end of the given list in the same order they appear in the input file.
If the file does not exist or cannot be opened, return false for failure. Otherwise, after importing the file, return true for success.
- bool CreateReportFile(const char* filename)
This function should create a grade report and write it to an output file (filename given by the parameter). Grade report file format is described below in this writeup.
If the output file cannot be opened, return false for failure. Otherwise, after writing the report file, return true for success.
- void ShowList() const
This function should print to the screen the current list of students, one student per line. The only information needed in this printout is last name, first name, and course name. Format this output so that it lines up in columns
Note that while this class will need to do dynamic memory management, you do not need to create a copy constructor or assignment operator overload (for deep copy) for this class. (Ideally we would, but in this case, we will not be using a StudentList object in a context where copies of such a list will be made)
4. Write a main program (in a separate file) that creates a single StudentList object and then implements a menu interface to allow interaction with the object. Your main program should implement the following menu loop (any single letter options should work on both lower and upper case inputs):
The import and export options should start by asking for user input of a filename (you may assume it will be 30 chars or less, no spaces), then call the appropriate class function for importing a file or printing the grade report to file, respectively. (If the inport/export fails due to bad file, print a message indicating that the job was not successfully completed).
The "Show student list" option should call the class function that prints the brief student list to screen (names and course info only).
The Show this Menu option should re-display the menu.
Quit should end the menu program. (Until this option is selected, keep looping back for menu selections).
5. File formats
Input File -- The first line of the input file will contain the number of students listed in the file. This will tell you how many student records are being imported from this file. After the first lines, every set of two lines constitutes a student entry. The first line of a student entry is the name, in the format lastName, firstName. Note that a name could include spaces -- the comma will delineate last name from first name. The second line will contain the subject ("English", "History", or "Math"), followed by a list of grades (all integers), all separated by spaces. There will be no extra spaces at the ends of lines in the file. The order of the grades for each class type is as follows:
English -- Attendance, Project, Midterm, Final Exam
History -- Term Paper, Midterm, Final Exam
Math -- Quiz 1, Quiz 2, Quiz 3, Quiz 4, Quiz 5, Test 1, Test 2, Final Exam
Output File -- The output file that you print should list each student's name (firstName lastName - no extra punctuation between), Final Exam grade, final average (printed to 2 decimal places), and letter grade (based on 10 point scale, i.e. 90-100 A, 80-89 B, etc). Output should be separated by subject, with an appropriate heading before each section, and each student's information listed on a separate line, in an organized fashion. (See example below). The order of the students within any given category should be the same as they appear in the student list. Data must line up appropriately in columns when multiple lines are printed in the file. At the bottom of the output file, print a grade distribution of ALL students -- how many As, Bs, Cs, etc.
General Requirements
No global variables, other than constants!
All member data of your classes must be private or protected
Use the const qualifier on member functions wherever it is appropriate.
The code for this program should be portable. Test with g++ compiler commands before submitting
You may use any of the standard I/O libraries that have been discussed in class (iostream, iomanip, fstream, as well as C libraries, like cstring, cctype, etc). You may also use the string class library
You may not use any of the other STL (Standard Template Libraries) besides string
Do not use any C++11-only libraries or features
Sample input files: (can download from links) testl.txt 4 Bunny, Bugs Math 90 86 80 95 100 99 96 93 Schmuckatelli, Joe History 88 75 90 Dipwart, Marvin English 95 76 72 88 Crack Corn, Jimmy Math 44 58 23 76 50 59 77 68 test2.txt 2 Kirk, James T. English 40 100 68 88 Lewinsky, Honica History 60 72 78Explanation / Answer
PROGRAM IN C++ :
main.cpp
using namespace std;
#include <iostream>
#include <cstring>
#include <cctype>
#include <fstream> //library importations
#include <iomanip>
#include <cstdlib>
#include "grades.h"
int main()
{
ifstream fin; //variable declerations
ofstream fout;
char ifile[80];
char ofile[80];
int size;
int i1, i2, i3, i4, i5, i6, i7, i8;
do
{
fin.clear(); //clears input stream after each loop run
cout << "Please enter the name of the input file. ";
cout << "Filename: ";
cin >> ifile; //takes in input file name
fin.open(ifile);
if(!fin) //if it doesn't open, b/c wrong name, prompts for new name
{
cout << "This is not a valid file. Try again!" << endl;
cout << "Please enter the name of the input file." << endl << "Filename: ";
cin >> ifile;
fin.open(ifile);
}
}while(!fin); //runs until correct opens
cout << "Please enter the name of the output file. ";
cout << "Filename: ";
cin >> ofile; //takes in name of output file from standard input
fin >> size; //reads size from file
fin.ignore(); //ignores
Student ** list = new Student * [size]; //creates Student array of pointers of size
for(int i = 0 ; i < size; ++i) //runs through each pointer in array
{
char x[20], y[20], z[20];
fin.getline(x,20,','); //stores last name into x up to ','
fin.ignore(); //ignores following space
fin.getline(y, 20); //stores first name into last up to ' '
fin.getline(z, 20, ' '); //takes in course into z up until a space
if(strcmp(z, "Math") == 0) //if the course is math
{
fin >> i1 >> i2 >> i3 >> i4 >> i5 >> i6 >> i7 >> i8; //reads in math data
list[i]=new Math(i1,i2,i3,i4,i5,i6,i7,i8); //dynamically allocates Math obj of math data
list[i]->setfirst(y); //sets obj member data to read in math data
list[i]->setlast(x);
list[i]->settype(z);
}
if(strcmp(z, "English")==0) //if the course is english
{
fin >> i1 >> i2 >> i3; //reads in enligh data
list[i]=new English(i1,i2,i3); //same as above
list[i]->setfirst(y);
list[i]->setlast(x);
list[i]->settype(z);
}
if(strcmp(z, "History")==0) //same as above for history
{
fin >> i1 >> i2 >> i3 >> i4;
list[i]=new History(i1,i2,i3,i4);
list[i]->setfirst(y);
list[i]->setlast(x);
list[i]->settype(z);
}
fin.ignore(); //ignores new line character, every time loop runs
}
fin.close(); //closes input file
fout.clear(); //clears output stream
fout.open(ofile); //opens output file
// capture current output stream settings
int oldprecision = fout.precision(); //stores variables for standard flags
ios_base::fmtflags oldflags = fout.flags();
// do my output changes
fout.setf(ios::right);
fout << fixed; //sets appropriate flags, precision
fout << setprecision(2);
fout << "Student Grade Summary" << endl;
fout << "--------------------- ";
fout << "ENGLISH CLASS ";
fout << "Student Final Final Letter ";
fout << "Name Exam Avg Grade ";
fout << "----------------------------------------------------------------- ";
for(int i=0; i <size; i++) //runs through array
{
if(strcmp(list[i]->gettype(), "English")==0) //if the pointer points to an English obj
{
//displays first name, last name, final exam, average in class, and letter grade
fout << list[i]->getfirst() << " " << list[i]->getlast() <<
(setw(43-(strlen(list[i]->getfirst()))-(strlen(list[i]->getlast())))) //sets spacing so its the same regardless of name length
<< list[i]->getfinalexam() << " " << list[i]->GetAverage() << " " << list[i]->getlettergrade() << " " << endl;
}
}
fout << " ";
fout << "HISTORY CLASS ";
fout << "Student Final Final Letter ";
fout << "Name Exam Avg Grade ";
fout << "----------------------------------------------------------------- ";
for(int i=0; i <size; i++)
{
if(strcmp(list[i]->gettype(), "History")==0) //same for history obj
{
fout << list[i]->getfirst() << " " << list[i]->getlast() <<
(setw(43-(strlen(list[i]->getfirst()))-(strlen(list[i]->getlast()))))
<< list[i]->getfinalexam() << " " << list[i]->GetAverage() << " " << list[i]->getlettergrade() << " " << endl;
}
}
fout << " ";
fout << "Student Final Final Letter ";
fout << "Name Exam Avg Grade ";
fout << "----------------------------------------------------------------- ";
for(int i=0; i <size; i++)
{
if(strcmp(list[i]->gettype(), "Math")==0) //same for math obj
{
fout << list[i]->getfirst() << " " << list[i]->getlast() <<
(setw(43-(strlen(list[i]->getfirst()))-(strlen(list[i]->getlast()))))
<< list[i]->getfinalexam() << " " << list[i]->GetAverage() << " " << list[i]->getlettergrade() << " " << endl;
}
}
fout << " ";
fout << "OVERALL GRADE DISTRIBUTION ";
int acount=0; //initializes count variables
int bcount=0;
int ccount=0;
int dcount=0;
int fcount=0;
for(int i=0;i<size;i++) //runs through array
{
if(list[i]->getlettergrade()=='A') //if the letter grade of pointed obj matches specific letter
acount++; //increase that counter
if(list[i]->getlettergrade()=='B')
bcount++;
if(list[i]->getlettergrade()=='C')
ccount++;
if(list[i]->getlettergrade()=='D')
dcount++;
if(list[i]->getlettergrade()=='F')
fcount++;
}
fout << "A: " << acount << endl; //displays distribution of counters
fout << "B: " << bcount << endl;
fout << "C: " << ccount << endl;
fout << "D: " << dcount << endl;
fout << "F: " << fcount << endl;
// PUT THINGS BACK THE WAY THEY WERE WHEN I FOUND THEM
fout.precision(oldprecision); // restore old precision setting
fout.flags(oldflags); // restore all prior format flags
fout.close(); //closes output file
for (int r=0; r < size; r++) //deletes dynamically allocated objs
{
delete list[r];
}
delete [] list; //deletes array of pointers (clears up space)
cout << "Processing complete ";
return 0;
}
grades.cpp
#include "grades.h"
#include <cctype>
#include <cstring>
#include <string>
#include <iomanip>
#include <iostream>
using namespace std;
//takes in test scores as paramaters, sets member data
Math::Math(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8)
{
quiz1=i1;
quiz2=i2;
quiz3=i3;
quiz4=i4;
quiz5=i5;
test1=i6;
test2=i7;
finalexam=i8;
}
//same
History::History(int i1, int i2, int i3, int i4)
{
attendance=i1;
project=i2;
midterm=i3;
finalexam=i4;
}
//same
English::English(int i1, int i2, int i3)
{
paper=i1;
midterm=i2;
finalexam=i3;
}
//copies paramater into member data
void Student::setfirst(char* x)
{
strncpy(first, x, 20);
}
void Student::setlast(char* y)
{
strncpy(last, y, 20);
}
void Student::settype(char* z)
{
strncpy(course, z, 20);
}
//returns member data
const char* Student::getfirst() const
{
return first;
}
const char* Student::getlast() const
{
return last;
}
const char* Student::gettype() const
{
return course;
}
//computers average member data for each class
double English::GetAverage()
{
englishavg=(paper*.25)+(midterm*.35)+(finalexam*.4);
return englishavg;
}
double Math::GetAverage()
{
quizavg=(quiz1+quiz2+quiz3+quiz4+quiz5)/5.0;
mathavg= (quizavg*.15)+(test1*.25)+(test2*.25)+(finalexam*.35);
return mathavg;
}
double History::GetAverage()
{
historyavg=(attendance*.1)+(project*.3)+(midterm*.3)+(finalexam*.3);
return historyavg;
}
//returns respective finalexam scores for each class
int English::getfinalexam()
{
return finalexam;
}
int History::getfinalexam()
{
return finalexam;
}
int Math::getfinalexam()
{
return finalexam;
}
//computes letter grade for each specific class based off their letter grade
char English::getlettergrade()
{
const double e = GetAverage();
if(e>=90)
return 'A';
else if(e>=80)
return 'B';
else if(e>=70)
return 'C';
else if(e>=60)
return 'D';
else
return 'F';
}
char History::getlettergrade()
{
const double e = GetAverage();
if(e>=90)
return 'A';
else if(e>=80)
return 'B';
else if(e>=70)
return 'C';
else if(e>=60)
return 'D';
else
return 'F';
}
char Math::getlettergrade()
{
const double e = GetAverage();
if(e>=90)
return 'A';
else if(e>=80)
return 'B';
else if(e>=70)
return 'C';
else if(e>=60)
return 'D';
else
return 'F';
}
grades.h
#include <cstring>
#include <cctype>
#include <fstream> //library importations
#include <iomanip>
#include <cstdlib>
#include <string>
class Student
{
public:
//manipulators of private member data
void setfirst(char* x);
void setlast(char* y);
void settype(char* z);
//retrivers of privatre member data
const char* gettype() const;
const char* getfirst() const;
const char* getlast() const;
//abstract virtual class, functions for child classes
virtual int getfinalexam()=0;
virtual char getlettergrade()=0;
virtual double GetAverage() = 0;
private:
protected:
char first[20];
char last[20];
char course[20];
};
class Math: public Student
{
public:
Math(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8); //constructor
//member functions
double GetAverage();
int getfinalexam();
char getlettergrade();
private:
int quiz1, quiz2, quiz3, quiz4, quiz5, test1, test2, finalexam;
double mathavg, quizavg;
protected:
};
class History: public Student
{
public:
History(int i1, int i2, int i3, int i4); //constructor
//member functions
double GetAverage();
int getfinalexam();
char getlettergrade();
private:
int attendance, project, midterm, finalexam;
double historyavg;
protected:
};
class English:public Student
{
public:
English(int i1, int i2, int i3); //constructor
//member functions
double GetAverage();
int getfinalexam();
char getlettergrade();
private:
int paper, midterm, finalexam;
double englishavg;
protected:
};
test.txt
6
Bunny, Bugs
Math 90 86 80 95 100 99 96 93
Schmuckatelli, Joe
English 88 75 90
Dipwart, Marvin
History 95 76 72 88
Crack Corn, Jimmy
Math 44 58 23 76 50 59 77 68
Kirk, James T.
History 40 100 68 88
Lewinsky, Monica
English 60 72 78
output.txt
Student Grade Summary
---------------------
ENGLISH CLASS
Student Final Final Letter
Name Exam Avg Grade
-----------------------------------------------------------------
Joe Schmuckatelli 90 84.25 B
Monica Lewinsky 78 71.40 C
HISTORY CLASS
Student Final Final Letter
Name Exam Avg Grade
-----------------------------------------------------------------
Marvin Dipwart 88 80.30 B
James T. Kirk 88 80.80 B
Student Final Final Letter
Name Exam Avg Grade
-----------------------------------------------------------------
Bugs Bunny 93 94.83 A
Jimmy Crack Corn 68 65.33 D
OVERALL GRADE DISTRIBUTION
A: 1
B: 3
C: 1
D: 1
F: 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.