I'm reading from a file into an array of objects i created called Team. The file is written in this format:

Shane Vitorino
CF
8
8
H
H
H
B
B
O
O
K
Ben Francisco
.......

The first line is the player's name, the second line is his position, the third is his number, the fourth is how many swings, and the rest are what those swings were and so forth for 16 total players. Here is my read function:

void readRoster(player rrTeam[], int count)
{
  ifstream fp;

  fp.open("player.dat");

  cout << endl << "Values are being read from file..." << endl;

  for (int i = 0; i < count; i++)
    {    
      string pn;
      string pos;
      int num;
      int numatBats = 0;
      int numbaseOnBalls = 0;
      int numstrikeOuts = 0;
      int numHits = 0;

      getline(fp, pn);
      rrTeam[i].setPlayerName(pn);
      fp >> pos; 
      rrTeam[i].setPosition(pos);
      fp >> num;
      rrTeam[i].setNumber(num);
      fp >> numatBats;
      rrTeam[i].setatBats(numatBats);

      for (int j = 0; j < numatBats; j++)
      {
       char temp;
       fp >> temp;
       if (temp == 'H')
        numHits++;
       else if(temp == 'K')
        numstrikeOuts++;
       else if(temp == 'B')
        numbaseOnBalls++;                      
       }
       rrTeam[i].setHits(numHits);
       rrTeam[i].setStrikeOuts(numstrikeOuts);
       rrTeam[i].setBaseOnBalls(numbaseOnBalls);
      }

 fp.close();                    
}

The 'idea' behind my loop is that it was supposed to read the player's information and record it up to the number of 'atBats', then count the hits, etc. of that player based on the number of atBats (via the second loop), record that, then start over again but for the next player (I.E rrTeam[1]) until it hits 15!

The code I have written gets ALL of the first player's information correctly (including the hits, etc.), but records nothing starting at the second player's name. Any help would be MASSIVELY appreciated! If I need any additional information to provide please let me know!

What are you passing in for "count"? ... Also, if you could use the code tags in the future(

), it would be much easier for us to read your code.

The 'idea' behind my loop is that it was supposed to read the player's information and record it up to the number of 'atBats', then count the hits, etc. of that player based on the number of atBats (via the second loop), record that, then start over again but for the next player (I.E rrTeam[1]) until it hits 15!

Initialize the variable count to 15.

I'm sorry about that! The 'count' variable is the size of the array, which is 16 (declared as a global variable). I hope this is better? I've changed it slightly from the above.

Right now my problem is that if I try and do cout << rrTeam[1].getPlayerName(); , it shows up as En Francisco, and all the future names are missing their first letter as well (the positions and numbers work fine however). A little bit of an improvement, but still having trouble with it!

EDIT: I know for (int j = 0; j <= numatBats; j++) should technically be: for (int j = 0; j < numatBats; j++) because it should run 8 times instead of 9, BUT changing it causes nothing to appear at all from cout! I originally changed it to <= out of curiosity, and found that it was at least I was getting PART of the second name via the <= even though it was clearly wrong!

void readRoster(player rrTeam[], int count)
{
  ifstream fp;
  
  fp.open("player.dat");
  
  cout << endl << "Values are being read from file..." << endl;
  
  for (int i = 0; i < count; i++)
    {    
      string pn;
      string pos;
      int num = 0;
      int numatBats = 0;
      int numbaseOnBalls = 0;
      int numstrikeOuts = 0;
      int numHits = 0;
      
      getline(fp, pn);
      rrTeam[i].setPlayerName(pn);
      getline(fp, pos);
      rrTeam[i].setPosition(pos);
      fp >> num;                   
      rrTeam[i].setNumber(num);
      fp >> numatBats;
      rrTeam[i].setatBats(numatBats);
     
      for (int j = 0; j <= numatBats; j++)
      {
       char temp;
       fp >> temp;
       if (temp == 'H')
        numHits++;
       else if(temp == 'K')
        numstrikeOuts++;
       else if(temp == 'B')
        numbaseOnBalls++;                      
       }
       rrTeam[i].setHits(numHits);
       rrTeam[i].setStrikeOuts(numstrikeOuts);
       rrTeam[i].setBaseOnBalls(numbaseOnBalls);
      }
      
      cout << rrTeam[1].getPlayerName();
  
 fp.close();
                    
}

The problem is the is a newline left after you get the last number. After line 41 add

fp.get();

Awesome! Thanks Nathan! That part works now, but unfortunately now I have issues with counting the hits, strikeouts, and baseonballs (its apparently giving me some wacky negative integer when I cout the getHits, etc., I should've probably tested that too before I posted here about the playername issue). I'll have to look at it later, work now :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.