Question: I'm writing code in C++ that is supposed to take a text file containing information on different football players and their combine results, store those
I'm writing code in C++ that is supposed to take a text file containing information on different football players and their combine results, store those players as a vector of objects of the class, and print out those objects. I'm running into a couple problems, which are shown below, along with my code.
MAIN.CPP:
#include "Combine.h" #includeusing namespace std; // main is a global function // main is a global function int main() { // This is a line comment //cout << "Hello, World!" << endl; vector players; getPlayersFromFile("CombinePlayers.txt", players); for (int i = 0; i < players.size(); i++) { cout << players[i] << endl; //cout << "Hello Jack" << endl; } return 0; }
COMBINE.H:
#ifndef COMBINE_H_ #define 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: //CONSTRUCTORS Combine() { name = "John Smith"; college = "University"; pos = "player"; height = -1; weight = -1; dash = -1; bench = -1; } 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); } //GETTERS 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; } //SETTERS 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 = -1; } else { this->height = height; } } void setWeight(int weight) { if (weight <= 0) { //Default value this->weight = -1; } else { this->weight = weight; } } void setDash(float dash) { if (dash <= 0.0) { //Default value this->dash = -1; } else { this->dash = dash; } } void setBench(float bench) { if (bench <= 0.0) { //Default value this->bench = -1; } else { this->bench = bench; } } // Overloaded operators 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; // Use ../ to get out of the cmake-build-debug folder to find the file inFile.open("../" + filename); // inFile.open(filename); string name = "", college = "", pos = "", header = ""; string temp; int height = 0, weight = 0; float dash = 0, bench = 0; char comma = ','; // check that the file is in a good state if (inFile) { // read in the header line getline(inFile, header); // loop through all the data in the file while (inFile && inFile.peek() != EOF) { // read line getline(inFile,temp); // pass it to string strem stringstream ss(temp); // get name getline(ss,name,','); // get college getline(ss,college,','); // get POS getline(ss,pos,','); // get height getline(ss,temp,','); height = atoi(temp.c_str()); // get weight getline(ss,temp,','); weight = atoi(temp.c_str()); if(ss.good()){ // get 40 yard getline(ss,temp,','); dash = atof(temp.c_str()); } if(ss.good()){ // get bench press getline(ss,temp,','); bench = atof(temp.c_str()); } // create a Combine object and put it on the back of the vector players.push_back( Combine(name, college, pos, height, weight, dash, bench)); } } else { cerr << "File not found" << endl; } inFile.close(); } #endif /* COMBINE_H_ */
CombinePlayers.txt:
Name, College, POS, Height (in), Weight (lbs), 40 Yard, Bench Press Johnathan Abram, Mississippi State, S, 71, 205, 4.45 Paul Adams, Missouri, OT, 78, 317, 5.18, 16 Nasir Adderley, Delaware, S, 72, 206 Azeez Al-Shaair, Florida Atlantic, LB, 73, 234, 16 Otaro Alaka, Texas A&M, LB, 75, 239, 4.82, 20
OUTPUT I'M GETTING:
Johnathan Abram Mississippi State S 71 205 4.45 -1 Paul Adams Missouri OT 78 317 5.18 16 Nasir Adderley Delaware S 72 206 5.18 16 Azeez Al-Shaair Florida Atlantic LB 73 234 16 16 Otaro Alaka Texas A&M LB 75 239 4.82 20
As we can see, i'm running into a particular problem. Missing data from the text file should be filled by a -1 in the output (as is the case with the first line/player Jonathan Abram, whose bench press is listed as -1 due to there being no data in the text file). However, subsequent lines don't follow this. In the case of Nasir Adderley (3rd line), their 40yard dash and bench should be listed as -1 as there was no listed data in the text file for those two; however, instead of -1s, we see the data from the previous line.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
