Question: C++ Data Type Question: For a project, I wrote code that takes a text file containing information on different football players and their combine results
C++ Data Type Question:
For a project, I wrote code that takes a text file containing information on different football players and their combine results and stores those players as a vector of objects of the class. Finally, I wrote a function that calculates the average weight of the players.
For a subsequent project, it's asking that I use the data type I created in this project. What data type did I create? I'm a bit confused on the wording/terminology maybe....
Below is my code from the project
Combine.h:
#include #include #include #include #include #include using namespace std; class Combine { private: string name, college, pos; int height, weight; float dash, bench; public: Combine() { name = "John Smith"; college = "University"; pos = "player"; height = 0; weight = 0; dash = 0; bench = 0; } Combine(string name, string college, string pos, int height, int weight, float dash, float bench) { this->name = name; this->college = college; this->pos = pos; setHeight(height); setWeight(weight); setDash(dash); setBench(bench); } string getName() const { return name; } string getCollege() const { return college; } string getPos() const { return pos; } int getHeight() const { return height; } int getWeight() const { return weight; } float getDash() const { return dash; } float getBench() const { return bench; } void setName(string name) { this->name = name; } void setCollege(string college) { this->college = college; } void setPos(string pos) { this->pos = pos; } void setHeight(int height) { if (height <= 0) { //Default value this->height = 0; } else { this->height = height; } } void setWeight(int weight) { if (weight <= 0) { this->weight = 0; } else { this->weight = weight; } } void setDash(float dash) { if (dash <= 0.0) { this->dash = 0; } else { this->dash = dash; } } void setBench(float bench) { if (bench <= 0.0) { //Default value this->bench = 0; } else { this->bench = bench; } } friend ostream& operator<<(ostream &outs, const Combine &com) { outs << left << setw(25) << com.name; outs << left << setw(25) << com.college; outs << left << setw(25) << com.pos; outs << right << setw(5) << com.height; outs << setw(5) << com.weight; outs << setw(5) << com.dash; outs << setw(5) << com.bench; return outs; } }; void getPlayersFromFile(string filename, vector &players) { ifstream inFile; inFile.open("../" + filename); string name = "", college = "", pos = "", header = ""; string temp; int height = 0, weight = 0; float dash = 0, bench = 0; char comma = ','; if (inFile) { getline(inFile, header); while (inFile && inFile.peek() != EOF) { getline(inFile,temp); stringstream ss(temp); getline(ss,name,','); getline(ss,college,','); getline(ss,pos,','); getline(ss,temp,','); height = atoi(temp.c_str()); getline(ss,temp,','); weight = atoi(temp.c_str()); if(ss.good()) { getline(ss,temp,','); dash = atof(temp.c_str()); } else{ dash=0; } if(ss.good()) { getline(ss,temp,','); bench = atof(temp.c_str()); } else{ bench=0; } players.push_back(Combine(name, college, pos, height, weight, dash, bench)); } } else { cerr << "File not found" << endl; } inFile.close(); } float avgWeight(vector &players) { float total = 0; getPlayersFromFile("CombinePlayers.txt", players); for (int i = 0; i < players.size(); ++i) { total += players[i].getWeight(); } cout << endl << "The average weight of a player is: " << (total/players.size()) << "lbs" << endl; } CombinePlayers.txt:
Name, College, POS, Height (in), Weight (lbs), 40 Yard, Bench Press Johnathan Abram, Mississippi State, S, 71, 205, 4.45, 0 Paul Adams, Missouri, OT, 78, 317, 5.18, 16 Nasir Adderley, Delaware, S, 72, 206, , 0 Azeez Al-Shaair, Florida Atlantic, LB, 73, 234, , 16 Otaro Alaka, Texas A&M, LB, 75, 239, 4.82, 20 Dakota Allen, Texas Tech, LB, 73, 232, 4.77, 23 Josh Allen, Kentucky, EDG, 77, 262, 4.63, 28 Zach Allen, Boston College, DE, 76, 281, 5, 24
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
