Floating point exception

I'm not sure on why this is happening. For some reason these values aren't being stored.
This is part of a PlayerDatabase class, that takes in a Pitcher or Hitter (both derived from the BaseballPlayer class) and the fields: totalInnings, totalEarnedRuns, totalEarnedRuns, totalEarnedRuns aren't being incremented. Anybody know what to do? In the base class I have each get function virtual and return 0. I think this is where my error is, but if I don't have those in the base class, I get an error saying that the function does not exist in the BaseballPlayer class. This is killing me...

while(!infile.eof())
{
if(playerType == 'P')
{
team[team_count] = new Pitcher();
team[team_count]->load_player(infile);
totalInnings += team[team_count]->get_inningsPitched();
totalEarnedRuns += team[team_count]->get_earnedRuns();
team_count++;
}
else
{
team[team_count] = new Hitter();
team[team_count]->load_player(infile);
totalAtBats += team[team_count]->get_atbats();
totalHits += team[team_count]->get_hits();
team_count++;
}
infile >> playerType;
}
Uh.. sorry, I really tried to understand what you are talking about, but I can't get your problem. The code you posted (btw: put it in [ code ] tags, looks much nicer) doesn't have any base class definition, you are speaking of. Maybe there is something wrong there? And how do you know your member variables are not incremented? Where are the total* variables defined? Maybe you are incrementing the wrong variables? Or they actally *are* incremented but you checking the wrong result?

General hint on how to debug "stuff that drives you crazy": Backup your current solution. Now start to simplifying. Remove everything that really should not matter. (Remove functions, classes, statements, variables etc.) until the problem is plain-simple and fits in like one screen. And most important, until the example is "self-contained", means everything to run and reproduce the problem is on the same code piece.

Chances are good, that you discover the problem on the way to the simple form. Believe me, it works.. ;-)

If you still have the problem with your simple, self-containing one-screen version, then you can easily post this self-containing code here (within [ code ] ... [ /code ] tags ;) ) and we can help you much much better.

Good luck bug hunting ;)

Ciao, Imi.

PS: The opposite way works too: Start from an empty class and add more and more elements from your current problem until it doesn't work as expected anymore.
You're right, it is rather difficult to understand the way a worded it. Ok. There is a PlayerDatabase class that holds an array of pointers to BaseballPlayer objects. In the PlayerDatabase class, I have variables to hold the total hits, at bats, innings pitched, and earned runs. Pitcher and Hitter are derived classes with BaseballPlayer as their base. The data for each player is taken from an input file. This code is in a member function of the PlayerDatabase class that will have each pointer in the array point to either a dynamically allocated Pitcher or Hitter. Each line of input from the file starts with a 'P' or 'H' to tell that this line contains the data for a Pitcher or Hitter. When the data for the player is taken in, I'm attempting total up all the data/stats as I add each player. When I run the print function, everything I need to print prints, except for the totals. I believe this is where my error is because I've added cout statements to the print function to see where things go wrong. All the variables: totalHits, totalAtBats, totalInnings, and totalEarnedRuns are equal to zero when printed to the screen.

while(!infile.eof())
{
if(playerType == 'P')
{
team[team_count] = new Pitcher();
team[team_count]->load_player(infile);
totalInnings += team[team_count]->get_inningsPitched();
totalEarnedRuns += team[team_count]->get_earnedRuns();
team_count++;
}
else
{
team[team_count] = new Hitter();
team[team_count]->load_player(infile);
totalAtBats += team[team_count]->get_atbats();
totalHits += team[team_count]->get_hits();
team_count++;
}
infile >> playerType;
}


As I said, its fastest when you try to simplify the code more and more until the code suddenly works.

The basic principle works for me:

1
2
3
4
5
6
7
8
9
10
struct PlayerBase { virtual ~PlayerBase(){}; virtual int get() = 0; }
struct Pitcher : PlayerBase { virtual int get(){return 1;} }

int main()
{
    int count = 0;
    PlayerBase b = new Pitcher();
    count += b->get();
    cout << count;
}


So there must be a program that is more complex than my code above but less complex than your code that almost doesn't work. Try to find this program - either by making my example more and more looking like your code or by making your code more and more like my example. I bet you will find the solution to your problem somewhere inbetween. (Maybe just a stupid typo.. ;)

If not, post the last *complete* (with all participating classes) simplification/complexiation step you could do ;-)
Topic archived. No new replies allowed.