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.