IN C ONLY The file datatxt holds the names and grades for th
**IN C++ ONLY**
The file data.txt holds the names and grades for the students in a math class. Your C++ program will calculate and display the average for each student, as well as their highest grade, and calculate and display the average for the whole class, as well as the highest grade.
• The number of grades for each student is unknown.
• The last ‘grade’ for each student will be -1, which is not an actual grade.
• The number of students is unknown. You must keep reading in student data until there is no more
• The class average is the average of the student’s averages.
• If a student has no grades (except for the -1), their average is 0.
• If there are no students in the class, the class average is 0.
Hint: You should not try to read in all the data from the file before doing any calculations or output. Much of it can be done one student at a time. Also, you will need to use a ‘nested’ loop, that is, a loop within a loop.
For example, if the data file contains the following data:
Sue Storm 95 76 89.3 99.2 91 -1
Reed Richards 76 85.5 89.25 -1
Bruce Banner 54.5 67.8 75 88.3 -1
The output will look like this:
The average for Sue Storm is: 90.1 and the high grade is: 99.2
The average for Reed Richards is: 83.6 and the high grade is: 89.3
The average for Bruce Banner is: 71.4 and the high grade is: 88.3
The average for the class is: 81.7 and the highest grade for the class is: 99.2
Solution
# include<iostream>
# include <fstream> //for file related operation in program
# include<vector> //for vector used in program
# include <algorithm> //for max_element() function in program
# include <string> //for string in program
# include <sstream> //for stringstream(breaking line of file into streams separated by space) in program
# include <iomanip> //for floating point precision in program
using namespace std;
//class for student
class student
{
public:
string FirstName,LastName;
void Load_and_calculate(string filename);
};
void student::Load_and_calculate(string filename)
{
ifstream myfile; //for Input file data.txt
std::string line;
float marks,avg,sum;
int i;
std::vector<float> class_avg; //vector for class average
std::vector<float> class_highest; //vector for highest in class
std::vector<float>::iterator max_ele;
myfile.open(filename.c_str()); //opening file data.txt
while (std::getline(myfile, line)) //reading file line by line
{
i=0;
std::vector<float> temp;
/*
the below line separates the line from file in string line into streams
for Ex:
for data.txt file
Sue Storm 95 76 89.3 99.2 91 -1
Reed Richards 76 85.5 89.25 -1
Bruce Banner 54.5 67.8 75 88.3 -1
std::getline(myfile, line) copies whole line from file into string line i.e.
line = Sue Storm 95 76 89.3 99.2 91 -1
and after std::stringstream ss(line);
ss will have
Sue
Storm
95
76
89.3
99.2
91
-1
i.e. into streams
*/
std::stringstream ss(line);
/*
Loop for reading info from above separated stream in ss;
i.e reading FIrstName and Lastname and then grades of subject
*/
while(1)
{
if(i==0)
ss>>FirstName;
else if(i==1)
ss>>LastName;
else
{
ss>>marks;
if(marks != -1)
temp.push_back(marks);
else
break;
}
i++;
}
sum=0.0;
for(unsigned int i=0;i<temp.size();i++)
{
sum += temp[i];
}
avg = (sum/temp.size()); //Average for individual student
class_avg.push_back(avg);
max_ele = std::max_element(temp.begin(),temp.end()); //High Grade for Individual Student
class_highest.push_back(*max_ele);
cout<<\"The average for \"<<FirstName<<\" \"<<LastName<<\" is: \"<<std::setprecision(3)<<avg<<\" and the high grade is: \"<<*max_ele<<endl;
}
sum=0;
for(unsigned int i=0;i<class_avg.size();i++)
{
sum += class_avg[i];
}
avg = sum/class_avg.size(); //Average for class
cout<<\"The average for the class is: \"<<std::setprecision(3)<<avg<<\" and the highest grade for the class is: \"<<*std::max_element(class_highest.begin(),class_highest.end())<<endl;
}
int main()
{
student S;
S.Load_and_calculate(\"data.txt\");
}


