Floating point exception

I am working on a function here that takes chars from a external file, converts them to ints and then uses those ints to calculate some stats. Here is the function, when I run the program I get a floating point exception error. This error seems to be caused by the calculations points =, GA = and GF = . Any input would be appriciated. Also let me know if it would help to post the rest of the program.


// read one line from input file,
// compute predictions for that team,
// and write them to the output file
// return false if the line contains invalid data,
// true otherwise
bool processLine(ifstream &inf, ofstream &outf)
{

string s, teamName, gamesPlayed, wins, losses, OTLosses, goalsFor, goalsAgainst;
points = 0;


// read each field as a string,
// (team name, games played, wins, losses,
// overtime losses, goals for, goals against)
// testing for end of file after each

// if the eof occurs after trying to read the team name
// return true (we've processed the last team, none remain)
// but if it occurs after any of the others return false
// (the last line had part of a team's stats but not the
// complete information, hence the file format is invalid)

// convert each field except the team name to an integer value
// using atoi (e.g. int GP = atoi(GPstring.c_str())
// and if any of them give a negative value return false
// since the stats cannot be valid

inf >> s;
s = teamName;
if (inf.eof())
return true;

inf >> s;
s = gamesPlayed;
int GP = atoi(gamesPlayed.c_str());
if ((inf.eof())|| (GP < 0))
return false;

inf >> s;
s = wins;
int W = atoi(wins.c_str());
if ((inf.eof()) || (W < 0))
return false;

inf >> s;
s = losses;
int L = atoi(losses.c_str());
if ((inf.eof())|| (L < 0))
return false;

inf >> s;
s = OTLosses;
int OTL = atoi(OTLosses.c_str());
if ((inf.eof()) || (OTL < 0))
return false;

inf >> s;
s = goalsFor;
int GF = atoi(goalsFor.c_str());
if ((inf.eof()) || (GF < 0))
return false;

inf >> s;
s = goalsAgainst;
int GA = atoi(goalsAgainst.c_str());
if ((inf.eof()) || (GA < 0))
return false;




// compute predicted points, goals for, goals against
// points = total games x (2 x wins + overtime losses)) / games played;
// goals for = (total games x goals for) / games played;
// goals against = (total games * goals against) / games played;



//================ERROR CAUSED BY THIS PORTION================================

points = GP * (2 * W + OTL) / GP;
GF = (GP * GF) / GP;
GA = (GP * GA) /GP;

//============================================================================

//to test output of calculations prior to writing them to the output file
cout << points;
cout << GF;
cout << GA;



// write results to the output file
/*

while (!inf.eof()){
inf >> s; // >> by default skips w/s so here we are getting a copy of the file with ws removed
outf <<s;
}


// pad the team string to 3 characters (e.g. using setw),
cout << setw(3) << left << teamName;

// the points, goals for, and goals against to 4 characters each
// (one team's stats per line)
cout << setw(4) << left << points;
cout << setw(4) << left << GF;
cout << sewt(4) << left << GA << endl;
*/





// return true for success
return true;
}
The problem is that you are dividing by zero.

1
2
3
inf >> s;
s = gamesPlayed;
int GP = atoi(gamesPlayed.c_str());

gamesPlayed is empty so atoi will return 0. Did you mean to write gamesPlayed = s;? It looks like the use of s here is not necessary. You can just use gamesPlayed directly.inf >> gamesPlayed;
Thanks, that cleared up the problem I was having with the floating point exception. Such a silly mistake.
Cheers!
Topic archived. No new replies allowed.