C++ problem
Author | Topic: C++ problem |
---|---|
Master
Member # 4614
|
written Tuesday, May 10 2005 16:46
Profile
Homepage
All right, here's the class declaration: And here's the function I'm using this class with: And my expected output: 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
|
written Tuesday, May 10 2005 17:09
Profile
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
|
written Tuesday, May 10 2005 17:25
Profile
Homepage
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
|
written Wednesday, May 11 2005 00:49
Profile
Homepage
quote:That looks odd to me, but I'm not up on C++ so I don't know. quote: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
|
written Wednesday, May 11 2005 02:34
Profile
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
|
written Wednesday, May 11 2005 13:12
Profile
Homepage
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 Aha, I changed the setAverage function to: 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
|
written Thursday, May 12 2005 22:52
Profile
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
|
written Friday, May 13 2005 00:05
Profile
*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
|
written Friday, May 13 2005 15:40
Profile
Homepage
*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 |