I wrote code in C++ that takes a text file containing information on different football players and their combine results, stores those players as a vector of objects of the class, and prints out those objects. Finally, I wrote a function that calculates the average weight of the players.
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; i < players.size(); ++i) { cout << players[i] << endl; //cout << "Hello Jack" << endl; }
avgWeight(players);
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()); } else{ dash=1; } if(ss.good()){ // get bench press getline(ss,temp,','); bench = atof(temp.c_str()); } else{ bench=1; } // 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(); }
float avgWeight(vector<Combine> &players) { float total = 0; getPlayersFromFile("CombinePlayers.txt", players); for (int i = 0; i < players.size(); ++i) { total += players[i].getWeight();//cout << "Hello Jack" << endl; } cout << (total/players.size()); }
#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:
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 1 1
Azeez
Al-Shaair
Florida
Atlantic
LB
73 234 16 1
Otaro
Alaka
Texas
A&M
LB
75 239 4.82 20
240.2
Now, this only worked because I stored any missing data as a 1 (so,
if the player did not have a recorded bench press number, it was
stored as 1). Thus, calculations were not impacted. However, I want
to be able to print that same data and perform the same
calculation, without storing missing data as a 1, but rather
leaving it blank. Is this possible?
In the above provided code has many syntax errors so i cannot provide you a working code , But I can provide you a logical solution
Solutions
1) Since you are using bench as data type of int it becomes impossible to set it to blank . Although in constructor you can initialize to -1 and while printing you can check if value is -1 then you can avoid printing the value to console
if(com.bench1=-1)
outs<<setw(5)<<com.bench;
2) change the datatype of all members in class from int to string, change all the getter setter type to string and initialize them with "" blank string in constructor, and while printing the answer check if string is not empty using
if(com.bench.compare("")!=0)
outs<<setw(5)<<com.bench;
also while computing the weights you need to convert string to integer , you can use below method to convert string to integer
#include<iostream
#include <sstream>
#include<string>
using namespace std;
int main()
{
string s=“134”;
stringstream stringToInteger(s);
int number = 0;
stringToInteger >> number;
cout<<number;
return 0;
}
you need to convert all the values in your strings to convert them to integer first before you can do avgWeight function
float avgWeight(vector<Combine> &players) { float total = 0; getPlayersFromFile("CombinePlayers.txt", players); for (int i = 0; i < players.size(); ++i) { string temp= player[i].getWeight(); //modify get weight to return string stringstream value(temp); int number=0; value>>number; total += number;//cout << "Hello Jack" << endl; } cout << (total/players.size()); }
I wrote code in C++ that takes a text file containing information on different football players...
Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...
Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem to get my code to work. The output should have the AVEPPG from highest to lowest (sorted by bubbesort). The output of my code is messed up. Please help me, thanks. Here's the input.txt: Mary 15 10.5 Joseph 32 6.2 Jack 72 8.1 Vince 83 4.2 Elizabeth 41 7.5 The output should be: NAME UNIFORM# AVEPPG Mary 15 10.50 Jack 72 8.10 Elizabeth 41 7.50 Joseph 32 6.20 Vince 83 4.20 My Code: #include <iostream>...
I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...
I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...
The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is wrong with the code written. I get errors about stockSym being private and some others after that.This was written in the Dev C++ software. Can someone help me figure out what is wrong with the code with notes of what was wrong to correct it? #include <cstdlib> #include <iostream> #include <iomanip> #include <fstream> #include <cassert> #include <string> using namespace std; template <class stockType> class...
I need to update this C++ code according to these instructions. The team name should be "Scooterbacks". I appreciate any help! Here is my code: #include <iostream> #include <string> #include <fstream> using namespace std; void menu(); int loadFile(string file,string names[],int jNo[],string pos[],int scores[]); string lowestScorer(string names[],int scores[],int size); string highestScorer(string names[],int scores[],int size); void searchByName(string names[],int jNo[],string pos[],int scores[],int size); int totalPoints(int scores[],int size); void sortByName(string names[],int jNo[],string pos[],int scores[],int size); void displayToScreen(string names[],int jNo[],string pos[],int scores[],int size); void writeToFile(string...
I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...
I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words); int main() { // Declaring variables std::map<std::string,int> words; //defines an input stream for the data file ifstream dataIn; string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...
Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...
The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...