Hey all, I'm writing a basketball stat program for my C++ class and I cant figure out where my bugs are coming from. I can get to the line that is s'pose to display the top off player name but it keeps giving me a segmentation fault it is on line 303. any suggestions on what I should do? thanks!
Code:#include <iostream>
#include <string>
#include <fstream>
#include <cassert>
#include <vector>
#include <iomanip>
#include <algorithm>
using namespace std;
bool getPlayer(int&, string&, string&, int&);
void getShooting(int&, int&, int&, int&, int&, int&, int);
void getRebounds(int&, int&);
void getOthers(int&, int&, int&, int&, int&);
void display();
void findTopOffensivePlayer();
void findTopDefensivePlayer();
void findMostValuablePlayer();
ifstream infile;
vector <int> number;
vector <int> minPlayed;
vector <int> fieldGoalsAttempts;
vector <int> fieldGoalsMade;
vector <int> threePointAttempts;
vector <int> threePointMade;
vector <int> freeThrowAttempts;
vector <int> freeThrowMade;
vector <int> offRebounds;
vector <int> defRebounds;
vector <int> steals;
vector <int> blocks;
vector <int> assists;
vector <int> turnOver;
vector <int> personalFoul;
vector <int> totalPoints;
vector <int> Points;
vector <double> pp48;
vector <string> firstName;
vector <string> lastName;
vector <double> threePointPercent;
vector <double> fieldGoalPercent;
vector <double> freeThrowPercent;
vector <double> offRating;
vector <double> defRating;
vector <double> MVPRating;
int main()
{
int num, min, fga, fgm, tpa, tpm, fta, ftm, off, def, stl, blk, ast, to, pf;
string first, last;
int totalMin = 0, totalFGA = 0, totalFGM = 0, totalTPA = 0, totalTPM = 0, totalFTA = 0, totalFTM = 0, totalOFF = 0, totalDEF = 0, totalSTL = 0, totalBLK = 0, totalAST = 0, totalTO = 0, totalPF = 0;
double tpp, fgp, ftp, pointsPer48, offensiveRating, defensiveRating;
cout << "Player Name Min FGA FGM PCT 3PA 3PM PCT FTA FTM PCT OFF DEF TOT STL BLK AST TO PF PTS P48" << endl;
while(getPlayer(num, first, last, min))
{
//push_back for getPlayer
number.push_back(num);
firstName.push_back(first);
lastName.push_back(last);
minPlayed.push_back(min);
//calculations for player
totalMin = min + totalMin;
getShooting(fga, fgm, tpa, tpm, fta, ftm, min);
//push_back for getShooting
fieldGoalsAttempts.push_back(fga);
fieldGoalsMade.push_back(fgm);
threePointAttempts.push_back(tpa);
threePointMade.push_back(tpm);
freeThrowAttempts.push_back(fta);
freeThrowMade.push_back(ftm);
//calculations for shooting
int points = (2 * fgm) + tpm + ftm;
Points.push_back(points);
if(points == 0 || min == 0)
{
pp48.push_back(0.0);
}
else
{
pointsPer48 = (double) points/(min/48);
pp48.push_back(pointsPer48);
}
if(fga == 0 || fgm == 0)
{
fieldGoalPercent.push_back(0.0);
}
else
{
fgp = double (fgm) / double (fga);
fieldGoalPercent.push_back(fgp);
}
if(tpa == 0 || tpm == 0)
{
threePointPercent.push_back(0.0);
}
else
{
tpp = double (tpm) / double (tpa);
threePointPercent.push_back(tpp);
}
if(fta == 0 || ftm == 0)
{
freeThrowPercent.push_back(0.0);
}
else
{
ftp = double (ftm) / double (fta);
freeThrowPercent.push_back(ftp);
}
totalFGA = fga + totalFGA;
totalFGM = fgm + totalFGM;
totalTPA = tpa + totalTPA;
totalTPM = tpm + totalTPM;
totalFTA = fta + totalFTA;
totalFTM = ftm + totalFTM;
getRebounds(off, def);
//push_back for getRebounds
offRebounds.push_back(off);
defRebounds.push_back(def);
//calculations for rebounds
totalOFF = off + totalOFF;
totalDEF = def + totalDEF;
getOthers(stl, blk, ast, to, pf);
//push_back for getOthers
steals.push_back(stl);
blocks.push_back(blk);
assists.push_back(ast);
turnOver.push_back(to);
personalFoul.push_back(pf);
//calculations for others
totalSTL = stl + totalSTL;
totalBLK = blk + totalBLK;
totalAST = ast + totalAST;
totalTO = to + totalTO;
totalPF = pf + totalPF;
int totalREBOUNDS = totalOFF + totalDEF;
int totalPoints = totalPoints + points;
Points.push_back(totalPoints);
double offensiveRating = (pointsPer48 + (off * .4) + (ast * .4) - (to * .4));
offRating.push_back(offensiveRating);
double defensiveRating = (def * .4) + (blk * .4) + (stl * .4) - pf;
defRating.push_back(defensiveRating);
double mostValueablePlayerRating = (offensiveRating * .33) + (defensiveRating * .67);
MVPRating.push_back(mostValueablePlayerRating);
display();
}
findTopOffensivePlayer();
findTopDefensivePlayer();
findMostValuablePlayer();
}
bool getPlayer(int& num, string& first, string& last, int& min)
{
if(!infile.is_open())
{
infile.open("lab3.dat");
}
infile >> num;
if(infile.eof())
{
infile.close();
return false;
}
else
{
assert(!infile.fail());
}
infile >> first;
assert(!infile.fail());
infile >> last;
assert(!infile.fail());
infile >> min;
assert(!infile.fail());
return true;
}
void getShooting(int& fga, int& fgm, int& tpa, int& tpm, int& fta, int& ftm, int min)
{
infile >> fga;
assert(!infile.fail());
infile >> fgm;
assert(!infile.fail());
infile >> tpa;
assert(!infile.fail());
infile >> tpm;
assert(!infile.fail());
infile >> fta;
assert(!infile.fail());
infile >> ftm;
assert(!infile.fail());
}
void getRebounds(int& off, int& def)
{
infile >> off;
assert(!infile.fail());
infile >> def;
assert(!infile.fail());
}
void getOthers(int& stl, int& blk, int& ast, int& to, int& pf)
{
infile >> stl;
assert(!infile.fail());
infile >> blk;
assert(!infile.fail());
infile >> ast;
assert(!infile.fail());
infile >> to;
assert(!infile.fail());
infile >> pf;
assert(!infile.fail());
}
void display()
{
for (int i = 0; i < firstName.size(); i++)
{
cout << firstName[i] << " " << lastName[i] << "\t\t" << minPlayed[i];
firstName.clear();
lastName.clear();
minPlayed.clear();
cout << " " << fieldGoalsAttempts[i] << " " << fieldGoalsMade[i] << " ";
fieldGoalsAttempts.clear();
fieldGoalsMade.clear();
cout << fixed << setprecision(2) << fieldGoalPercent[i];
fieldGoalPercent.clear();
cout << " " << threePointAttempts[i] << " " << threePointMade[i];
threePointAttempts.clear();
threePointMade.clear();
cout << fixed << setprecision(2) << threePointPercent[i];
threePointPercent.clear();
cout << " " << freeThrowAttempts[i] << " " << freeThrowMade[i] << " ";
freeThrowAttempts.clear();
freeThrowMade.clear();
cout << fixed << setprecision(2) << freeThrowPercent[i];
freeThrowPercent.clear();
cout << " " << offRebounds[i] << " " << offRebounds[i];
offRebounds.clear();
defRebounds.clear();
cout << " " << steals[i] << " " << blocks[i] << " " << assists[i] << " " << turnOver[i] << " " << personalFoul[i];
steals.clear();
blocks.clear();
assists.clear();
turnOver.clear();
personalFoul.clear();
cout << " " << Points[i] << " " << pp48[i] << endl;
Points.clear();
pp48.clear();
}
}
void findTopOffensivePlayer()
{
assert(offRating.size() > 0);
int offIndex = 0;
for (int i = 0; i < offRating.size(); i++)
{
if (offRating[i] > offRating[offIndex])
{
offIndex = i;
}
}
cout << offIndex << endl;
cout << firstName[offIndex] << endl;
}
void findTopDefensivePlayer()
{
assert(defRating.size() > 0);
int defIndex = 0;
for (int i = 0; i < defRating.size(); i++)
{
if (defRating[i] > defRating[defIndex])
{
defIndex = i;
}
}
cout << defIndex << endl;
//cout << firstName[defIndex] << endl;
}
void findMostValuablePlayer()
{
assert(MVPRating.size() > 0);
int mvpIndex = 0;
for (int i = 0; i < MVPRating.size(); i++)
{
if (MVPRating[i] > MVPRating[mvpIndex])
{
mvpIndex = i;
}
}
cout << mvpIndex << endl;
cout << firstName[mvpIndex] << endl;
}