#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    1
    Rep Power
    0

    This output won't make sense


    I'm writing a prog where derived classes inherit a number of arrays holding names. For some reason, it works fine for the first name, last name, but spits out garbage for team name, and the prog crashes after that. I'm stumped- can anyone tell me what I'm missing?



    PHP Code:

    class athlete {
    protected:
        
    char *firstname;
        
    char *lastname;
        
    char *team;
        
    int jersey;
        
    int gplayed;
    public:
        
    athlete(const char*, const char*, const char*, int=0int=0);

        
    virtual  void calcFigs()const=0;
     
    };
    athlete::athlete(const char *first , const char *last , const char *team1int jint g)
    {
        
    firstname=new char[strlen(first)+1];
        
    strcpy(firstnamefirst);
        
    lastname=new char[strlen(last)+1];
        
    strcpy(lastnamelast);
        
    team =new char[strlen(team1)+1];
        
    jersey=j;
        
    gplayed=g;}

    class 
    bball : public athlete{
    public:
        
    bball(const char*, const char*, const char*, int=0int=0int=0int=0int=0);
        
        
    virtual void calcFigs()const;
    private:
        
    int rebounds;
        
    int assists;
        
    int points;




    };
    bball::bball(const char *first, const char *last, const char *team1int jrint gplint reb,
                 
    int ***, int poi) : athlete(first last team1){
        


        
    jersey=jrgplayed=gpl;
        
    rebounds=rebassists=***;
        
    points=poi;
    }
    void bball:: calcFigs()const 
    {
        
        
    float avgpavgaavgr ;
        
    avgp = (  points/gplayed);
        
    avga = (  assists/gplayed);
        
    avgr = (  rebounds/gplayed);


        
    cout<<firstname<<" "<<lastname //crashes here 
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    You don't copy the contents of team1 into the variable.

    Also, in the future, please use an appropriate subject title.
    Last edited by Onslaught; May 21st, 2003 at 09:48 AM.
  4. #3
  5. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    Here is what I come up with (using VC++ 6)
    This compiled and worked as expected.
    PHP Code:
    #include <iostream>
    #include <string.h>

    using namespace std;

    class 
    athlete {
    protected:
        
    char *firstname;
        
    char *lastname;
        
    char *team;
        
    int jersey;
        
    int gplayed;
    public:
        
    athlete(const char*, const char*, const char*, int=0int=0);
        ~
    athlete();
        
    virtual  void calcFigs()const=0;
    };

    athlete::athlete(const char *first , const char *last , const char *team1int jint g) {
        
    athlete::firstname = new char[strlen(first) + 1];
        
    strcpy(athlete::firstnamefirst);

        
    athlete::lastname = new char[strlen(last) + 1];
        
    strcpy(athlete::lastnamelast);

        
    athlete::team = new char[strlen(team1) + 1];
        
    strcpy(athlete::teamteam1);

        
    athlete::jersey=j;
        
    athlete::gplayed=g;
    }

    athlete::~athlete() {
        
    delete [] athlete::firstname;
        
    delete [] athlete::lastname;
        
    delete [] athlete::team;
    }

    class 
    bball : public athlete {
    public:
        
    bball(const char*, const char*, const char*, int=0int=0int=0int=0int=0);
        
    virtual void calcFigs()const;
    private:
        
    int rebounds;
        
    int assists;
        
    int points;
    };

    bball::bball(const char *first, const char *last, const char *team1int jrint gplint reb,
        
    int astint poi) : athlete(first last team1) {
        
    bball::jersey=jr;
        
    bball::gplayed=gpl;
        
    bball::rebounds=reb
        
    bball::assists=ast;
        
    bball::points=poi;
    }

    void bball::calcFigs()const {
        
    float avgpavgaavgr ;
        
    avgp = (points/gplayed);
        
    avga = (assists/gplayed);
        
    avgr = (rebounds/gplayed);

        
    cout << bball::firstname << " " << bball::lastname << " for " << bball::team << endl;
        
    cout << "Average Points: " << avgp << endl;
        
    cout << "Average Assists: " << avga << endl;
        
    cout << "Average Rebounds: " << avgr << endl;
    }

    int main() {
        
    bball J("M","J","Chicago",24,2,12,20,32);
        
    J.calcFigs();
        
    getchar();
        return 
    1;

  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    1
    Rep Power
    0

    Re: This output won't make sense


    Originally posted by dorquemada
    I'm writing a prog where derived classes inherit a number of arrays holding names. For some reason, it works fine for the first name, last name, but spits out garbage for team name, and the prog crashes after that. I'm stumped- can anyone tell me what I'm missing?
    Yes. An original username.

IMN logo majestic logo threadwatch logo seochat tools logo