In: Computer Science
Please in C++ thank you! Please also include the screenshot of the output. I have included everything that's needed for this. Pls and thank you!
Write a simple class and use it in a vector.
Begin by writing a Student class. The public section has the following methods:
Student::Student()
Constructor. Initializes all data elements: name to empty string(s), numeric variables to 0.
bool Student::ReadData(istream& in)
Data input. The istream should already be open. Reads the following data, in this order:
• First name (a string, 1 word)
• Last name (also a string, also 1 word)
• 5 quiz scores (integers, range 0-20)
• 3 test scores (integers, range 0-100)
Assumes that all data is separated by whitespace. The method returns true if reading all data was successful, otherwise returns false. Does not need to validate or range-check data; if one of the quiz or test scores is out of range, just keep going.
bool Student::WriteData(ostream& out) const
Output function. Writes data in the following format. Each student’s data is on one line.
• First name (left justified, 20 characters)
• Last name (left justified, 20 characters) • 5 quiz scores (each right justified in a field 4 characters wide)
• 3 test scores (each right justified in a field 5 characters wide)
• new-line character ‘\n’ or use of endl in output function
Note that this is a const method; it should not modify any of the object’s data. Returns true if the attempt to send the data to the stream was successful, false otherwise.
string Student::GetFirstName() const;
string Student::GetLastName() const;
Accessor methods, also known as ‘getters.’ Returns data from inside the function.
float Student::CourseAverage() const
Returns the student’s weighted average as a percentage (float in range 0-100). The quiz scores are averaged together (20-point scale) and are 35% of the course grade. The test are averaged together and are 65% of the course grade. This does not modify any of the object’s data.
bool Student::DisplayCourseAverage(ostream& out) const Prints the student’s course average to the provided output stream, rounded to 3 decimal places. Returns true if the attempt was successful, false otherwise.
string Student::LetterGrade() const Returns a string with the student’s letter grade, using the same grading scale as this course. The class should assume that any input or output streams passed to public methods are already open.
The main program is quite simple. You are provided a data file with data for some number of students. Read the data into a vector. (Hint: Declare one student. Read that student’s data, then add it to the vector. Repeat.) It should then print a roster, showing the name, course average to 3 decimal places, and letter grade for each student in the course. This list should be sorted by student name (sort by last name, break ties using first name; display in usual first-name last-name order).
Here is the input.txt
23
MIRANDA BOONE 17 17 13 17 20 86 91 71
LARRY MARSH 16 11 20 15 11 78 82 96
JANIE FOWLER 18 18 19 18 20 80 90 69
ALBERTO GILBERT 15 20 18 17 19 51 76 99
SUE LEONARD 12 15 12 18 17 85 77 100
CARL BALLARD 16 12 16 16 20 65 80 92
LESLIE PEREZ 13 18 17 12 20 81 81 96
MORRIS NEWTON 20 19 19 20 16 75 80 66
BRANDI LAMB 19 17 20 12 16 97 67 65
HERBERT HARVEY 20 16 11 14 12 80 100 80
RUDOLPH FISHER 17 18 20 18 18 82 68 77
LAURIE PHELPS 18 11 13 16 15 90 93 76
PHILIP ZIMMERMAN 17 13 18 16 18 79 78 64
BRAD HAYES 17 20 13 11 20 81 92 87
BELINDA JACKSON 18 10 16 18 16 59 76 87
JESSIE MORAN 16 14 13 19 19 78 79 66
BRIDGET KELLER 19 14 15 20 20 90 66 66
CHAD RODRIGUEZ 15 16 20 18 18 86 71 79
KENNETH KELLY 11 19 17 19 18 76 75 67
CASSANDRA MASON 14 20 18 20 17 85 76 100
RANDY JACKSON 18 19 15 19 17 67 61 65
GARRY CLAYTON 10 16 18 14 19 86 85 84
NICK FLOYD 18 13 20 17 16 79 76 81
Program
#include<iostream>
#include<fstream>
#include<vector>
#include<iomanip>
using namespace std;
//Student class
class Student
{
private:
//private data members
string firstName;
string lastName;
int quizScore[5];
int examScore[3];
public:
//public functions
Student();
bool ReadData(istream& in);
bool WriteData(ostream& out) const;
string GetFirstName() const;
string GetLastName() const;
float CourseAverage() const;
bool DisplayCourseAverage(ostream& out) const;
string LetterGrade() const;
};
//constructor
Student::Student()
{
firstName = "";
lastName = "";
for(int i=0; i<5; i++)
quizScore[i] = 0;
for(int i=0; i<3; i++)
examScore[i] = 0;
}
//function to read the data
bool Student::ReadData(istream& in)
{
in>>firstName;
if(in.fail())
return false;
in>>lastName;
if(in.fail())
return false;
for(int i=0; i<5; i++){
in>>quizScore[i];
if(in.fail())
return false;
}
for(int i=0; i<3; i++){
in>>examScore[i];
if(in.fail())
return false;
}
return true;
}
//function to write the data
bool Student::WriteData(ostream& out) const
{
out<<left<<setw(20)<<firstName;
if(out.fail())
return false;
out<<left<<setw(20)<<lastName;
for(int i=0; i<5; i++)
out<<right<<setw(4)<<quizScore[i];
for(int i=0; i<3; i++)
out<<right<<setw(5)<<examScore[i];
out<<endl;
return true;
}
//function to return the firstName
string Student::GetFirstName() const
{
return firstName;
}
//function to return the lastName
string Student::GetLastName() const
{
return lastName;
}
//function to calculate course average
float Student::CourseAverage() const
{
float avg1, avg2, sum = 0;
for(int i=0; i<5; i++)
sum += quizScore[i];
avg1 = sum/5;
sum = 0;
for(int i=0; i<3; i++)
sum += examScore[i];
avg2 = sum/3;
return (avg1*35/20 + avg2*65/100);
}
//function to display course average
bool Student::DisplayCourseAverage(ostream& out) const
{
out<<fixed<<setprecision(3);
out<<left<<setw(20)<<CourseAverage();
if(out.fail())
return false;
return true;
}
//function to return the letter grade
string Student::LetterGrade() const
{
float ca = CourseAverage();
if (ca >= 93 && ca <= 100) return "A";
if (ca >= 90) return "A-";
if (ca >= 87) return "B+";
if (ca >= 83) return "B";
if (ca >= 80) return "B-";
if (ca >= 77) return "C+";
if (ca >= 73) return "C";
if (ca >= 70) return "C-";
if (ca >= 67) return "D+";
if (ca >= 60) return "D";
return "F";
}
//function to sort the vector
void sorting(vector<Student> &sv)
{
//sort the vector according to the last name
for(int i=0; i<sv.size()-1; i++){
for(int j=i+1; j<sv.size(); j++){
if(sv[i].GetLastName() > sv[j].GetLastName() ||
(sv[i].GetLastName() == sv[j].GetLastName() &&
sv[i].GetFirstName() > sv[j].GetFirstName())){
Student stu = sv[i];
sv[i] = sv[j];
sv[j] = stu;
}
}
}
}
//main function
int main()
{
ifstream fin;
//open the file
fin.open("Program4Data.txt");
//check if file exist
if(!fin.is_open())
{
cout<<"File not exist!"<<endl;
return 1;
}
//declare vector of Student
vector<Student> sv;
Student stu;
//read data from the file and add to the vector
while(stu.ReadData(fin))
{
sv.push_back(stu);
}
//sort the vector
sorting(sv);
//display the roaster
for(int i=0; i<sv.size(); i++)
{
cout<<left<<setw(20)<<sv[i].GetFirstName();
cout<<left<<setw(20)<<sv[i].GetLastName();
cout<<left<<setw(20)<<sv[i].CourseAverage();
cout<<sv[i].LetterGrade()<<endl;
}
return 0;
}
Output:
Solving your question and
helping you to well understand it is my focus. So if you face any
difficulties regarding this please let me know through the
comments. I will try my best to assist you.
Thank you.