Like Tree2Likes
  • 1 Post By AndrewHunter
  • 1 Post By laserlight

Homework help

This is a discussion on Homework help within the C++ Programming forums, part of the General Programming Boards category; Hey all, I'm writing a basketball stat program for my C++ class and I cant figure out where my bugs ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    3

    Homework help

    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;
    }

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    1. Read Why global variables are bad.
    2. All your stats are for a player, you should make a struct player and then a vector of those structs.
    3. If you are allowed to use vectors, you should access vector elements via iterators, it is much safer.
    4. You included <algorithm> so use it, specifically std::copy and stream iterators to read your data in. Again safer and easier.

    That is what I have for now. Make those changes and repost if you still have problems.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Nuzut View Post
    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.
    How do you know that? If it is just because of the cout output, keep in mind cout is buffered and you could be many lines past 303 and seg fault, but before the data subsequently added to the cout buffer is actually flushed to the console.

    If that is how you deduced line 303, change your couts to cerrs (stderr is not buffered) and try again.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    3
    I just assumed that because when I comment out that line it works, sorta.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Here, I worked up a quick example to demonstrate some of the concepts I talked about. You should be allowed to implement these things based on the includes it appears you are allowed to use.
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>
    #include <iterator>
    
    struct name{
    	std::string first;
    	std::string last;
    };
    
    std::istream& operator>>(std::istream& stream, name& data){
    	return(stream >> data.first >> data.last);
    }
    std::ostream& operator<<(std::ostream& stream, name& data){
    	return(stream << data.first <<" "<<data.last<<std::endl);
    }
    void makefile(void);
    
    int main(void){
    
    	std::vector<name>vect_names;
    	
    	makefile();
    	std::ifstream file("example.txt");
    	if(!file.is_open())
    		exit(0);
    
    	std::copy(std::istream_iterator<name>(file),
    		std::istream_iterator<name>(),
    		std::back_inserter(vect_names));
    	file.close();
    
    	std::vector<name>::iterator name_it = vect_names.begin();
    	while(name_it != vect_names.end()){
    		std::cout << *name_it;
    		++name_it;
    	}
    
    	return(0);
    }
    
    void makefile(void){
    
    	std::ofstream file("example.txt");
    	if(!file.is_open())
    		exit(0);
    
    	file << "John " <<"Doe\n";
    	file << "Jane " <<"Doe\n";
    
    	file.close();
    }
    Last edited by AndrewHunter; 09-24-2011 at 12:03 PM. Reason: darn tokens....
    Alexandre likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    If you are going to read using std::copy, then you might as well print using it too, e.g.,
    Code:
    std::copy(vect_names.begin(),
              vect_names.end(),
              std::ostream_iterator<name>(std::cout));
    Incidentally, the declaration of operator<< is not const-correct. The data parameter should be a const name& instead.
    AndrewHunter likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by laserlight View Post
    If you are going to read using std::copy, then you might as well print using it too, e.g.,
    Code:
    std::copy(vect_names.begin(),
              vect_names.end(),
              std::ostream_iterator<name>(std::cout));
    Thanks for pointing that out Laser, I was just trying to demonstrate different methods; guess I should have mentioned it.

    Quote Originally Posted by laserlight View Post
    Incidentally, the declaration of operator<< is not const-correct. The data parameter should be a const name& instead.
    Good catch. Thanks!
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Homework plz!
    By mythassassin in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2007, 03:07 PM
  2. Homework... help!
    By xMEGANx in forum C++ Programming
    Replies: 11
    Last Post: 10-15-2007, 05:36 AM
  3. Help with C++ Homework
    By TheTaoOfBill in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2007, 07:17 PM
  4. ok, yes, I need help with my homework!
    By melee in forum C Programming
    Replies: 5
    Last Post: 09-22-2004, 07:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21