Code Review Needed - Function Causing Core Dump (Code in URLs)

Hello C++.com,

I'm relatively new to coding (a few months), and I need some help figuring out how to fix this program I'm writing for an assignment. Most of the grunt work is actually done, and I can compile, but when I run, I get a memory dump.

To explain the purpose of the program, we are given two files; one file has a list of teams names and team IDs (sample here: https://coderpad.io/485235 ), and another file with team IDs with either scores for a game or a bye, separated by week (sample here: https://coderpad.io/260478 ). From these, the program is supposed to come up with a sorted week-by-week list similar to this:

Rankings after week #N 
RANKING: Team_Name (W-L-T) 
RANKING: Team_Name (W-L-T) 
RANKING: Team_Name (W-L-T) 
BLANKLINE 


As for the actual program, the only requirements are that the program is made of three files; team.h ( https://coderpad.io/313159 ), team.cpp ( https://coderpad.io/108960 ), and main.cpp ( https://coderpad.io/485332 ). Additionally, team.h must contain a class called Team with member arrays for name, ID, wins, losses, and ties, but any additional member variables or member functions are on my perogative. I can assume that I will only be given up to fifty teams to add into the arrays.

Now, the issue I have is that somewhere within either function Team::getScores or Team::setResults, some command is causing a 'segmentation fault (core dump)' and I don't exactly know what the issue is. I have suspicion it could be my loop implementation, but I do not know where to look to use any debug cout statements. I was hoping that perhaps someone a bit more experienced could help me figure out what the issue is.

In advance, thank you for your time, and I hope I can get a response soon.

Per Laborem ad Astra,
JbstormburstADV
Last edited on
1
2
3
4
5
6
  while ( !inStream.eof() ) {
    inStream >> m_teamName >> m_teamID;  //catches the last input twice
    m_teamName = teamName[teamNumber];
    m_teamID = teamID[teamNumber];
    teamNumber++;
  }
The reason is that you test for eof before you actually read the data. In other words: when inStream >> m_teamName >> m_teamID; raises an eof you add it to your data none the less. If an error in the stream occurs the variable is not modified by the stream and hence you get the last content twice.

Better post the code here with code tags: [code]your code[/code]
I've actually been doing a bit more experimentation, and that isn't it. When I switched the order of the variables in lines 3 and 4 of what you posted above, I was able to properly call the values. Instead, now, thanks to some editing and usage of gdb, I know I'm getting stuck in this if-else at some point:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 if ( init != '-' ) {
      inStream >> teamID1;
      while ( teamID1 != teamID[teamA] ) {
        teamA++;
        continue;
      }
      inStream >> score1;
      if ( score1 == 'B') {
        teamBye[teamA]++;
        teamA = 0;
        inStream.ignore( 2 );
      }
      else {
        inStream >> teamID2;
        while ( teamID2 != teamID[teamB] ) {
          teamB++;
        }
        inStream >> score2;
        if ( static_cast<int> (score1) > static_cast<int> (score2) ) {
          teamWin[teamA]++;
          teamLoss[teamB]++;
        }
        else if ( static_cast<int> (score1) < static_cast<int> (score2) ) {
          teamWin[teamB]++;
          teamLoss[teamA]++;
        }
        else if ( static_cast<int> (score1) == static_cast<int> (score2) ) {
          teamTies[teamA]++;
          teamTies[teamB]++;
        }
      }
    }


Knowing my luck, I suspect that when I get to a BYE, I'm not getting redirected to line 8, but end up somewhere else. Should I use ifstream.peek to look for B first, then read score2, in this case?
Last edited on
Topic archived. No new replies allowed.