Pregunta

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:

  1. 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++;        
   }
¿Fue útil?

Solución

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.

Otros consejos

like 0x499602D2 user commented - your InputPlayerData() method is not returning any value.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top