C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 21st, 2003, 02:55 AM
dorquemada dorquemada is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 1 dorquemada User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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 

Reply With Quote
  #2  
Old May 21st, 2003, 08:16 AM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,840 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 36 m 16 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
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 08:48 AM.

Reply With Quote
  #3  
Old May 21st, 2003, 08:45 AM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,840 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 36 m 16 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
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;


Reply With Quote
  #4  
Old May 30th, 2003, 04:30 PM
dorquemada1 dorquemada1 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 1 dorquemada1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Re: This output won't make sense

Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > This output won't make sense


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT