I've looked at a couple similar questions but I still can't seem to come close to understanding this. I get the basic idea for structs and array but I'm so lost for actually creating a function to read data from a file into an array of structs. Hoping someone could help me out. :(
We have to:
- Write a function, InputPlayerData that will read the data from the input file of unknown >length into the array of structs
A. The data in the input file contains the following information about each player
name
position
number (Player's number on her jersey)
at bats (The number of times a player is at bat)
hits (The number of hits while at bat)
- bases taken (The number of bases run after a hit)
B. Sample lines of data from the input file:
i. The data for the first player:
MarTee Apple is the player’s name
Catcher is the player’s position
17 is her jersey number
20 is the number of times at bat
7 is the number of hits
20 is the number of bases taken
So, that's what it looks like. I tried to start the function and the program actually runs but I don't get the output. The cursor just blinks. Any help would be so much appreciated.
struct playerRecordType
{
string name;
string position;
int number;
int atBats;
int hits;
int basesTaken;
int ranking;
double battingAverage;
double sluggingAverage;
};
int InputPlayerData(ifstream& inFile, playerRecordType player[MAX]);
int main(void)
{
ifstream inFile;
ofstream outFile;
//open the files
inFile.open("SoftballData.txt");
outFile.open("SoftballResults.txt");
playerRecordType player[MAX];
int length = InputPlayerData(inFile, player);
outFile << length;
return 0;
}
int InputPlayerData(ifstream& inFile, playerRecordType player[])
{
int index;
//initialize i
index = 0;
//primer for the loop
getline(inFile, player[index].name);
//while not end-of-file to process the file data
while(!inFile.eof())
{
inFile >> player[index].name >> player[index].position
>> player[index].number >> player[index].atBats
>> player[index].hits >> player[index].basesTaken;
index++;
}