Create a Student class which will contain the following:1)A String object to hol
ID: 3856594 • 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 grades3)A method to read in the Student class from a file formatted as:a)Name on one line by itselfb)Five integer values for the grades on one line delineated by commas4)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.TO TURN IN:1)An electronic copy of the .cpp and .h files in the project folder as created by Visual Studio.
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Student
{
public:
string studentName[25];
int marks[25];
int grades[5];
const int size=25;
int debug=size;
ifstream infile;
void WriteData()
{
infile.open("input.txt");
while(size==debug)
{
cout<<" Enter the grades of the Student ";
for(int j=0;j<5;j++)
{
cin>>grades[j];
infile>>grades[j];
}
for(int i=0;i<size;i++)
{
cout<<"Enter A Student Name ";
cin>>studentName[i];
infile>>studentName[i];
cout<<" Enter Marks ";
cin>>marks[i];
infile>>marks[i];
}
debug--;
}
}
void DisplayData()
{
int sum=0;
for(int i=0;i<size;i++)
{
sum+=marks[i];
}
for(int i=0;i<size;i++)
{
cout<<"Student Name is "<<studentName[i];
cout<<" Marks "<<marks[i];
cout<<" Average is "<<sum/size;
cout<<" ";
}
cout<<" Student Grades are ";
for(int i=0;i<5;i++)
{
cout<<" "<<grades[i];
}
}
};
int main()
{
Student obj;
obj.WriteData();
obj.DisplayData();
return 0;
}
Output:
Note: Here i am displaying for only two student records
Enter the grades of the Student
5
6
7
8
9
Enter A Student Name Hemanth
Enter Marks 45
Enter A Student Name Ravi
Enter Marks 67
Student Name is Hemanth Marks 45 Average is 56
Student Name is Ravi Marks 67 Average is 56
Student Grades are
5
6
7
8
9