#include <iostream>
#include <sstream>
#include <fstream> //needed for reading the file
#include <string> //needed for std::string
#include <vector> //needed for creating a vector of Avenger objects
//this class represents a Person
class Avenger
{
public:
std::string name;
double highest = 0, average = 0;
};
int main()
{
//lets create a vector of Avenger objects
std::vector<Avenger> vec;
//open the file
std::ifstream inputFile("input.txt");
std::string line;
//check if file opened successfully
if(inputFile)
{
//read line by line from input file
while(std::getline(inputFile, line, '\n'))
{
Avenger object;//create Avenger object
std::istringstream ss(line);
std::string word;
//read word by word from line
ss >> word; //take the first name into word
//add the first name to the name data member of object
object.name = std::move(word);
ss >> word; //take the last name into word
//add the last name to the name data member
object.name += " " + word;
int count = 0;//this will be used to count the number of items
double value;
while((ss >> value) && (value !=-1))
{
object.average+= value;
if(value > object.highest)
{
object.highest = value;
}
++count;
}
object.average = object.average/count;
vec.emplace_back(object);
}
}
else
{
std::cout<<"File cannot be opened"<<std::endl;
}
inputFile.close();
//lets go through the vector and print out the details
for(const Avenger& elem: vec)
{
std::cout<<"The average for "<<elem.name<<" is "<<elem.average<<" and the highest score for "<<elem.name<<" is "<<elem.highest<<std::endl;
}
}
Steve Rogers 85 97.34 94 78 -1
Bruce Banner 67 35 51 68 -1
Charles Xavier 91.5 68.3 87.4 82 96.2 -1
Natasha Romanov 85 87.2 96.25 78.9 -1
Peter Parker 99 87.5 80 95.2 97.6 -1