First, this:
while(!inFile.eof())
is wrong. Read this article for why this is so.
Next, your loop body and initial entrance is incorrect. You do this:
//primer for the loop
getline(inFile, player[index].name);
in an apparent attempt to load the first player's name, then proceed in the loop body to do this:
inFile >> player[index].name >> player[index].position
>> player[index].number >> player[index].atBats
>> player[index].hits >> player[index].basesTaken;
Note the player's name line is read twice. This will throw off the remaining data of this record, with the last element, basesTaken
attempting to extract (and fail) to pull an integer from the second player's name. The result is the stream will be placed in an error state, but has not yet reached EOF. Therefore, adding insult to injury the incorrect while-loop condition discussed previously will never be true, the loop will execute again, and due to the error state, fail all extractions, looping again, not yet eof, etc...
I would hazard to guess this is what you're trying to do:
#include <iostream>
#include <fstream>
#include <string>
struct playerRecordType
{
std::string name;
std::string position;
int number;
int atBats;
int hits;
int basesTaken;
int ranking;
double battingAverage;
double sluggingAverage;
};
inline std::istream& operator >>(std::istream& inp, playerRecordType& player)
{
if (std::getline(inp, player.name) &&
std::getline(inp, player.position) &&
(inp >> player.number >> player.atBats >> player.hits >> player.basesTaken))
{
if (player.atBats > 0)
{
player.battingAverage = static_cast<double>(player.hits)/
static_cast<double>(player.atBats);
player.sluggingAverage = static_cast<double>(player.basesTaken)/
static_cast<double>(player.atBats);
}
else
{
player.battingAverage = 0;
player.sluggingAverage = 0;
}
};
return inp;
}
template<std::size_t N>
std::size_t InputPlayerData(std::istream& inp, playerRecordType (&players)[N])
{
std::size_t x = 0;
for (; x < N && inp >> players[x]; ++x);
return x;
}
#define MAX (100)
int main()
{
std::ifstream inFile("SoftballData.txt");
playerRecordType players[MAX];
std::size_t length = InputPlayerData(inFile, players);
std::cout << length << '\n';
return 0;
}
I leave the output side of this little quest into the deep blue code as an exercise for you. Best of luck.