The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
This output won't make sense
Discuss This output won't make sense in the C Programming forum on Dev Shed. This output won't make sense C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 21st, 2003, 02:55 AM
|
|
Junior Member
|
|
Join Date: May 2003
Posts: 1
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=0, int=0);
virtual void calcFigs()const=0;
};
athlete::athlete(const char *first , const char *last , const char *team1, int j, int g)
{
firstname=new char[strlen(first)+1];
strcpy(firstname, first);
lastname=new char[strlen(last)+1];
strcpy(lastname, last);
team =new char[strlen(team1)+1];
jersey=j;
gplayed=g;}
class bball : public athlete{
public:
bball(const char*, const char*, const char*, int=0, int=0, int=0, int=0, int=0);
virtual void calcFigs()const;
private:
int rebounds;
int assists;
int points;
};
bball::bball(const char *first, const char *last, const char *team1, int jr, int gpl, int reb,
int ***, int poi) : athlete(first , last , team1){
jersey=jr; gplayed=gpl;
rebounds=reb; assists=***;
points=poi;
}
void bball:: calcFigs()const
{
float avgp, avga, avgr ;
avgp = ( points/gplayed);
avga = ( assists/gplayed);
avgr = ( rebounds/gplayed);
cout<<firstname<<" "<<lastname //crashes here
|

May 21st, 2003, 08:16 AM
|
 |
/(bb|[^b]{2})/
|
|
Join Date: Nov 2001
Location: Somewhere in the great unknown
|
|
|
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.
|

May 21st, 2003, 08:45 AM
|
 |
/(bb|[^b]{2})/
|
|
Join Date: Nov 2001
Location: Somewhere in the great unknown
|
|
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=0, int=0);
~athlete();
virtual void calcFigs()const=0;
};
athlete::athlete(const char *first , const char *last , const char *team1, int j, int g) {
athlete::firstname = new char[strlen(first) + 1];
strcpy(athlete::firstname, first);
athlete::lastname = new char[strlen(last) + 1];
strcpy(athlete::lastname, last);
athlete::team = new char[strlen(team1) + 1];
strcpy(athlete::team, team1);
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=0, int=0, int=0, int=0, int=0);
virtual void calcFigs()const;
private:
int rebounds;
int assists;
int points;
};
bball::bball(const char *first, const char *last, const char *team1, int jr, int gpl, int reb,
int ast, int 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 avgp, avga, avgr ;
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;
}
|

May 30th, 2003, 04:30 PM
|
|
Junior Member
|
|
Join Date: May 2003
Posts: 1
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|