Hey guys,
Just wondering if someone can help me out with this. I want to read in a text file (team.txt) and have it implemented into the rest of my code. My program will read in from the file, the number of players and their heights and print out some statistics. Here is what I have so far:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct player {
    string name;
    float height; // player’s height in feet
};
/* In the functions below, players represents a pointer to
a dynamically allocated array of players, and num_players is the size
(number of elements) in that array. */
float averageHeight(const player *players, int num_players);
string tallest(const player *players, int num_players);
void readPlayers(ifstream &inputf, player **playersp, int *num_playersp);
int main() {
    ifstream inputf("team.txt");
    int num_players(0); // the number of players in team.txt
    player *players(0); // players[i] is the the ith player read from team.txt
    readPlayers(inputf, &players, &num_players);
    cout << num_players << " players read\n";
    cout << "The average player is " << averageHeight(players, num_players)
        << "feet tall\n";
    cout << "The tallest player is " << tallest(players, num_players) << "\n";
    delete players;
}
//void readPlayers(ifstream &inputf, player **playersp,int //*num_playersp){
//    ifstream f("team.txt");
    int a;
    float b;
    f >> a;
    f >> b;
    *num_playersp = f;

    **playersp = new int["team.txt"];
}

I want to implement this readPlayers function into my code, along with others but I will get to that later. readPlayers will read the number of players from the input file and assign that through the num_playersp pointer. Then allocate an array of players and assign that through the playersp pointer. Finally read in the heights from the file into the array allocated.
I am having trouble with this part, as I am not too skilled with dynamically allocated arrays and reading in input files. Does anyone have any suggestions? Comments? Help? Anything would be greatly appreciated.

OK, back up the train a bit. int num_players(0); is not the way to initialize a simple data type. Try:

int num_players = 0;

The array of player structs needs be only a 1D array, so your **playersp is excess.

The input function prototype ought to be more like:

void readPlayers(ifstream &inputf, player *playersp, int &num_playersp);

The num_players need not be a pointer in main( ), just an int passed by reference. Better yet, why isn't it a return value from the function?

What is this supposed to be doing?

**playersp = new int["team.txt"];

Tell us what you're trying to accomplish there, and we'll work on setting you on the correct path.

Well answering your last question the **playersp = new int ["team.txt"] was just random typing trying to get my thoughts/ideas out. Hoping something would piece together. Im not sure why, but it seems to help me. As for my goal, I am trying to read in from a text file called team.txt. Once I read in the number of players from this file, I want to assign that through the num_playersp pointer. There are other steps after this but they are basically copy paste from this first portion. For example, the text file could look like this:
3
John 7.0
Mary 5.0
Mike 5.8

The names are the strings and the heights are the floats. I guess for right now I am trying to read in the number of players, and pass it through the num_playersp pointer. Does that clear up somethings?

Actually, I was pretty clear what you probably were trying to do, I wanted to see if you had it straight.

So, your input function needs to do several things.
1 - allocate the variable to read in the num_players. That will be used to control a reading loop and will be returned to the caller.

2 - allocate player structs to the players pointer (maybe a name that gives a bit of distinction would be handy - how about "team"?)

3 - read from the input stream, storing the appropriate members of the team elements.

Can you put those into concrete code statements?

So far I came up with this:

void readPlayers(player){
    int i =0;
    inputf >> num_playersp;
    while(!inputf.eof()){
        inputf >> playersp[i].name;
        inputf >> playersp[i].height;
    }

The loop will find the line, read it and get the name and height of each player. The inputf reads the first line, which is the number of players in the file.
The text file looks like this:
3
John 7.0
Mary 5.0
Mike 5.8

Is this on the right track? Or does what I typed not do what Im intending it to do?

Go back to my first post - it gave the function header, which is based on your original which was very close to correct. You need to pass to the function the input stream, the player pointer, and, as you seem to want, the reference to the count of players.

In the function, read the number of players.

Then, allocate the player structs array based on that number.

I'll leave you to search a bit on this forum for why your use of eof() is not quite correct, and not a good choice for controlling the input loop. The number of players should be used to control that loop, assuming the file data will be correct.

Allocating dynamic array, an example.

struct foo 
{
    int a;
    char name[20];
};

foo *array;

array = new foo[10];

cin >> array[0].a;
cin >> array[0].name;

OK, WaltP pointed you to the eof discussion. Read and heed.

Edited 6 Years Ago by vmanes: n/a

Isnt that last piece of code what I have here?
After changing the eof, does this work?

void readPlayers(ifstream &inputf, player *playersp, int &num_playersp){
    int i =0;
    
    inputf >> num_playersp;
    while(inputf){
        inputf >> playersp[i].name;
        inputf >> playersp[i].height;
    }

It doesnt give me any errors, I just get 1 unresolved external and a LNK external error.

You are not paying attention to all that's been said previously

void readPlayers(ifstream &inputf, player *playersp, int &num_playersp){
    int i =0;
    
    inputf >> num_playersp; //ok, you know how many players

    //but you need to allocate that many elements to the *playersp

    while(inputf)  // this is really no better than using eof( )
    {
        inputf >> playersp[i].name;  
        inputf >> playersp[i].height;
    }

Your code will read past end of file, and you will attempt to write to a record that doesn't exist.

A better means of controlling the input loop is to use an input action as the loop condition. When there's no more input, the value of that statement is 0, or NULL.

ifstream f("data.txt");
   int data;
   while( f >> data  )
   {
       cout << data << endl;
    }
   f.close( );

Since you know how many records there should be, and you should have sized the array to hold just that many, you really need a dual control on the loop - while (you have a place to put stuff && while there's something there to store)

ifstream f("data.txt");
   int data[10] = { 0 };
   int i = 0;

   while( i < 10 &&  f  >> data[i] )
   {
       cout <<data[i] << endl;
       i++;
   }

   f.close( );
This article has been dead for over six months. Start a new discussion instead.