C++ problem

Error message

Deprecated function: implode(): Passing glue string after array is deprecated. Swap the parameters in drupal_get_feeds() (line 394 of /var/www/pied-piper.ermarian.net/includes/common.inc).
AuthorTopic: C++ problem
Master
Member # 4614
Profile Homepage #0
All right, here's the class declaration:
class player
{
public:
player();
player(int g, int ab, int hit);
~player(){}
void setAverage(){average=(hits*1000)/AB;}
int getAverage() const {return average;}
int getGames() const {return gamesPlayed;}
int getAb() const {return AB;}
int getHits() const {return hits;}

private:
int average;
int gamesPlayed;
int AB;
int hits;
};


player::player():
average(0),
gamesPlayed(0),
AB(0),
hits(0){}

player::player(int g, int ab, int hit)
{
gamesPlayed=g;
AB=ab;
hits=hit;
}
And here's the function I'm using this class with:
void PlayerStats()
{
int i;
player players[9];
cout<<" Name Average G Hits AB \n";
cout<<" -----------------------------------------\n";
for(i=0;i<9;i++)
{
cout<<" Player "<<i+1<<" ";
players[i].setAverage();
int ave=players[i].getAverage();
cout<<".";
if(ave<100)
{
cout<<"0";
if(ave<10)
cout<<"0";
}
cout<<ave<<" "<<players[i].getGames()<<" "<<players[i].getHits()<<
" "<<players[i].getAb()<<"\n";
}
cout<<"\n\n";
}
And my expected output:
Name Average G Hits AB
-----------------------------------------
Player 1 .000 0 0 0
Player 2 .000 0 0 0
Player 3 .000 0 0 0
Player 4 .000 0 0 0
Player 5 .000 0 0 0
Player 6 .000 0 0 0
Player 7 .000 0 0 0
Player 8 .000 0 0 0
Player 9 .000 0 0 0
Sorry for my lack of empty lines where there should have been; it wouldn't put them in for me.

Now the problem: instead of creating the expected output when I call this function, the only thing I get is "Player 1," and then one of those program error boxes comes up that says I need to close the program and that "Microsoft needs to hear about this problem," to which you click "Don't Send" (XP users know what I'm talking about).

Do any of you programmers see anything fatally wrong with this that might be causing the error, or is my code just plain illegible? :)

Oh, and this comes from my program I'm working on to track baseball statistics, since people were wondering.

[ Tuesday, May 10, 2005 16:47: Message edited by: 1001011001000 ]

--------------------
-ben4808

For those who love to spam:
CSM Forums
RIFQ
Posts: 3360 | Registered: Friday, June 25 2004 07:00
Shock Trooper
Member # 4445
Profile #1
I can't see the problem, but the way I would handle it is thusly: make another project that uses the class, and comment out all of the functions except the constructors and the destructor. From there, make a main function where you just declare an instance. Then, uncomment each member function and test it in main(). If everything goes as planned, then the problem is not with your class.

That said, I don't know if you can make arrays of objects. In my C++ class, we use a "vector" class, which is basically an array, and you can't use objects with it. Anyways, I'd use a linked list that writes to and reads from a data file, if I were you.

EDIT: I also think you need to declare ave up where you declare i. I don't know if it's not allowed, but it makes for ugly code, so I've never done it or seen it done.

[ Tuesday, May 10, 2005 17:11: Message edited by: PoD person ]
Posts: 293 | Registered: Saturday, May 29 2004 07:00
Master
Member # 4614
Profile Homepage #2
I have tested this with two separate player objects, player1 and player2 using the overloaded constructor, and they worked just fine. However, the array is what seems to get me into trouble. The array is good, though, because it allows me to use a for loop in the function to list each player separately, but all in one block. Without the array, I'd have to rewrite the the block inside the for loop nine times, which would make the code cumbersome and repetitive.

So PoD, you talked about using a vector to declare that array that might work better. How exactly would you do that?

Oh and on a side note, if I don't set the default constructor to set all the member variables to 0 and just had it create the object, I can avoid the error until the Player 7 line. But the lines above are filled with weird numbers, suggesting the member variables are getting assigned to something I didn't assign them to. With the default constructor shown, however, the program works as I said before...I doesn't. :P

I'm on Dev C++, if that helps at all. :/

--------------------
-ben4808

For those who love to spam:
CSM Forums
RIFQ
Posts: 3360 | Registered: Friday, June 25 2004 07:00
Post Navel Trauma ^_^
Member # 67
Profile Homepage #3
quote:
player::player():
average(0),
gamesPlayed(0),
AB(0),
hits(0){}
That looks odd to me, but I'm not up on C++ so I don't know.

quote:
void setAverage(){average=(hits*1000)/AB;}
Division by zero here, methinks.

I suggest you run the program in a debugger.

--------------------
Barcoorah: I even did it to a big dorset ram.

desperance.net - Don't follow this link
Posts: 1798 | Registered: Thursday, October 4 2001 07:00
Shaper
Member # 32
Profile #4
Those are for initilization; they look okay at the moment. However the problem is the division by zero. Since it's clear the setAverage function causes it. It may be nicer in this case to use a vector of players, that way you can access the size of the vector in the for loop. This also allows you to increase the number of players you are looking at.

[ Wednesday, May 11, 2005 02:35: Message edited by: Lt. Sullust ]

--------------------
Lt. Sullust
Cogito Ergo Sum
Polaris
Posts: 2462 | Registered: Wednesday, October 3 2001 07:00
Master
Member # 4614
Profile Homepage #5
Ooh, yikes, you guys are right! Division by 0. That's what causes all those goofy numbers too. :eek: Thanks a lot; I'll try to fix that.

Khoth, I was just initializing all the member variables in the default contructor in the initialization phase instead of the body. Altervatively, I could have written
player::player()
{
average=0;
gamesPlayed=0;
AB=0;
hits=0;
}
Aha, I changed the setAverage function to:
void player::setAverage()
{
if(AB==0){average=0;}
else{average=(hits*1000)/AB;}
}
It works with just the right expected output too! Thanks everyone! :) :) :)

[ Wednesday, May 11, 2005 13:30: Message edited by: N00BEN ]

--------------------
-ben4808

For those who love to spam:
CSM Forums
RIFQ
Posts: 3360 | Registered: Friday, June 25 2004 07:00
Warrior
Member # 5091
Profile #6
Piece of advice:

Remember the words of rands. BONGHITS WILL FIX YOUR SOURCE CODE. MAKE SURE TO STUFF YOUR BONG WITH RAT POISON FIRST TOO.
Posts: 180 | Registered: Friday, October 15 2004 07:00
La Canaliste
Member # 5563
Profile #7
*gently smacks Djur's wrists for using bad words*

--------------------
I am a pale shadow of the previous self.
quote:

Deep down, you know you should have voted for Alcritas!
Posts: 387 | Registered: Tuesday, March 1 2005 08:00
Master
Member # 4614
Profile Homepage #8
*smacks Djur's wrists much harder*

I haven't seen you around lately, though, Djur.

--------------------
-ben4808

For those who love to spam:
CSM Forums
RIFQ
Posts: 3360 | Registered: Friday, June 25 2004 07:00