Create a Student class which will contain the following:1)A String object to hol
ID: 3857702 • Letter: C
Question
Create a Student class which will contain the following:1)A String object to hold a student’s last name2)An array object to hold five integers representingthe student’s grades 3)A method to read in the Student class from a file formatted as: a) Name on one line by itself b) Five integer values for the grades on one line delineated by commas 4)A method to display the students name, grades, and average.An Array class will be used to hold the collection of Student objects. This Array class will have a method to sort the Student records by name.Demonstrate the correct working of your classes by reading in a collection of student records from a file, sort the records, and print them. If a name is “EOF” it will mark the end of the file.There will be a maximum of 25 student records in the file, although it may be less.
some test Data:
Penny
75,85,95,100,44
Paul
100,100,100,100,100
Fred
44,55,66,77,88
Aron
99,88,77,66,55
EOF
-------
so far what i have to read in file in main:
#include <fstream>
#include <iostream>
using namespace std;
void main ()
{
fstream MyFile;
char Line [81];
MyFile.open ("TestFile1.txt", ios_base::in);
while (!MyFile.is_open ()) // did the file fail to open?
{
MyFile.clear (); // if anything failed, you need to clear the errors
// if a problem occurs, nothing about the file will work until you do clear
MyFile.open ("TestFile.txt", ios_base::in);
}
do {
MyFile.getline (Line, 81);
cout << Line << endl;
} while (strcmp (Line, "EOF") != 0);
MyFile.close (); // make sure to close the file when you are finished using it
cout << "Bye" << endl;
}
Explanation / Answer
#include<iostream>
#include<string>
#include<cstdlib>
#include<cmath>
#include<fstream>
#include<sstream>
using namespace std;
//STUDENT CLASS
class Student
{
//VARIABLES TO HOLD STUDENT NAME AND 5 SUBJECTS GRADES
string studname;
int grades[5];
public:
//CONSTRUCTOR
Student()
{
studname=" ";
}
//METHOD TO SET STUDENT NAME AND GRADES
void setValue(string name,int inGrades[5])
{
studname=name;
for(int kk=0;kk<5;kk++)
{
grades[kk]=inGrades[kk];
}
}
//GET STUDENT NAME
string getstudName()
{
return studname;
}
//FIND THE AVERAGE OF GRADES
float findstudavg()
{
int sum=0;
for(int kk=0;kk<5;kk++)
{
sum+=grades[kk];
}
return (float)sum/(float)5;
}
//DISPLAY STUDENT NAME,GRADES, AVERAGES
void printstudinfo()
{
cout<<endl;
cout<<"Student name:"<<studname<<endl;
cout<<"grades:";
for(int kk=0;kk<5;kk++)
{
cout<<grades[kk]<<" ";
}
cout<<" "<<"student avg:"<<findstudavg()<<endl;
}
};
//ARRAY CLASS TO SORT THE
class Array
{
//ARRAY OF STUDENT OBJECTS
Student stud[25];
int cnt;
public:
//CONSTRUCTOR
Array()
{
cnt=0;
}
//READ STUDENT NAME AND GRADES FROM FILE
void readFromstudfile()
{
string tempname;
int inGrades[5];
string line;
int aa;
int t;
ifstream fin("studfile.txt");
if(fin.is_open())
{
while(getline(fin,tempname))
{
if(tempname=="EOF")
break;
getline(fin,line);
stringstream ss(line);
aa=0;
while(ss>>t)
{
inGrades[aa]=t;
aa++;
if(ss.peek()==',')
ss.ignore();
}
stud[cnt].setValue(tempname,inGrades);
cnt++;
}
}
fin.close();
}
//SORT STUDENTS BY LAST NAME
void sortstudentbylastname()
{
Student temp;
int aa,bb;
for(aa=1;aa<cnt;aa++)
{
temp=stud[aa];
bb=aa-1;
while(bb>=0&&stud[bb].getstudName()>temp.getstudName())
{
stud[bb+1]=stud[bb];
bb=bb-1;
}
stud[bb+1]=temp;
}
}
//DISPLAY THE ALL STUDENT DETAILS
void displayStudObjects()
{
cout<<"------------------------------"<<endl;
for(int kk=0;kk<cnt;kk++)
{
stud[kk].printstudinfo();
}
}
};
//MAIN METHOD
int main()
{
//ARRAY TO HOLD STUDENT OBJECTS
Array arr;
//READ THE STUDENT DETAILS FROM FILE
arr.readFromstudfile();
cout<<"Student Info before sorting"<<endl;
//DISPLAY ALL STUDENT DETAILS BEFORE SORTING
arr.displayStudObjects();
//CALL METHOD TO SORT THE STUDENTS BY LAST NAME
arr.sortstudentbylastname();
//DISPLAY STUDENTS DETAILS AFTER SORTING
cout<<"Student Info after sorting"<<endl;
arr.displayStudObjects();
system("pause");
return 0;
}