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. **EDIT** I did change the dash variable to a float instead of an int, but have the same output

MAIN.CPP:

#include "Combine.h" #include  using 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; players.size(); i++) { cout << players[i] << endl; //cout << "Hello Jack" << endl;   } return 0; }

COMBINE.H:

#ifndef PROJECT1_COMBINE_H #define PROJECT1_COMBINE_H  #include  #include  #include  #include  #include   using namespace std; class Combine { private: string name, college, pos; int height, weight, 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, int dash, int 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; } int getDash() const { return dash; } int 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(int dash) { if (dash <= 0) { //Default value  this->dash = -1; } else { this->dash = dash; } } void setBench(int bench) { if (bench <= 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); string name = "", college = "", pos = "", header = ""; int height = 0, weight = 0, 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 name  getline(inFile, name, ','); // read college  getline(inFile, college, ','); // read pos  getline(inFile, pos, ','); // read height  inFile >> height >> comma; // read weight  inFile >> weight >> comma; // read 40 yard dash  inFile >> dash; // if the file is not in a good state, then there wasn't an integer to read in.  if (!inFile) { // set a default value to class2  dash = 0; // clear the file stream  inFile.clear(); } // read in the comma  inFile >> comma; // read bench press  inFile >> bench; // if the file is not in a good state, then there wasn't an integer to read in.  if (!inFile) { // set a default value to class2  bench = 0; // clear the file stream  inFile.clear(); } // read in the comma  inFile >> comma; // create a Combine object and put it on the back of the vector  players.push_back(Combine(name, college, pos, height, weight, dash, bench)); } inFile.close(); } #endif //PROJECT1_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:

Johnathan Abram Mississippi State S 71 205 4 45 aul Adams Missouri OT 78 317 5 18 16 Nasir Adderley Delaware S 72 206 -1 -1 ez Al-Shaair Florida Atlantic LB 73 234 16 -1 aro Alaka Texas A&M LB 75 239 4 82 20 Texas A&M LB 75 239 -1 -1 0 0 0 0 0 0 0 0

So there are two issues going on in the output. 1.) After the first object, each player's name is cutoff by 2-3 characters in the beginning. 2.) The 40 yard dash numbers are read as two different numbers due to the period (so 4.82 becomes 4 and 82).

If you need more information/context, let me know!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!