// team.cpp -- Driver for the player class. // This program illustrates an array of class objects. // Since each object of the player class is a football player, // an array of these objects can be considered a football team. // // The program reads a file containing some data on members of a // football team. Each player is assigned to a single player object. // The entire team becomes an array of these player objects. // The program prints the player information in an easy-to-read format, // using the player class << operator. // // Note that at the top of the program, we define p to be an array // of 50 player objects. We may not need all these, so we keep track // of how many players are read from the file with the "num_players" variable. #include "player.h" #include #include void print_team(player p[], int num_players); int main() { int i = 0, num_players; player p[50]; ifstream in_file; char file_name[81], line[81]; cout << "What is the name of the input file? "; cin >> file_name; in_file.open(file_name); while (in_file.fail()) { cout << "Can't find the file. Try again.\n"; cout << "What is the name of the input file? "; cin >> file_name; in_file.open(file_name); } // This is the loop that reads each player's numbers from the file. while (1) { in_file.getline(line, 81); if (in_file.eof()) break; player temp(line); p[i++] = temp; } num_players = i; // Once we have the entire array of player objects initialized, // let's show the user the entire team. cout << "\nHere are the members of the football team:\n\n"; print_team(p, num_players); return 0; } // This function just prints all the objects to the screen. // Note that we are invoking the << operator for each object. void print_team(player p[], int num_players) { for (int i = 0; i < num_players; ++i) if (p[i].over200()) cout << p[i]; } // Assume that the file team.txt looks like this, // containing exactly these lines of text: // // Alessio, Steven 27 6-8 233 11/8/1971 // Blumfield, Justin 32 6-2 185 24/11/1977 // Bolton, Mark 23 6-4 189 3/3/1979 // Denham, Sean 38 5-10 176 29/4/1969 // Fletcher, Dustin 6-6 202 92 5/7/1975 // Henneman, Aaron 3 6-4 191 13/12/1980 // Hird, James 5 6-2 202 2/4/1973 // Jacobs, Danny 42 6-2 198 25/6/1980 // Lloyd, Matthew 18 6-3 198 16/4/1978 // McAlister, Daniel 4 6-1 191 22/8/1978 // Wallis, Dean 21 6-3 200 27/8/1969 // Wellman, Sean 6 6-4 196 20/9/1974 // // The output of the program looks like this: // // What is the name of the input file? team.txt // // Here are the members of the football team: // // # 27 Steven Alessio 6' 8" 233 lbs. born 11/ 8/1971 // # 32 Justin Blumfield 6' 2" 185 lbs. born 24/11/1977 // # 23 Mark Bolton 6' 4" 189 lbs. born 3/ 3/1979 // # 38 Sean Denham 5' 10" 176 lbs. born 29/ 4/1969 // # 31 Dustin Fletcher 6' 6" 202 lbs. born 5/ 7/1975 // # 3 Aaron Henneman 6' 4" 191 lbs. born 13/12/1980 // # 5 James Hird 6' 2" 202 lbs. born 2/ 4/1973 // # 42 Danny Jacobs 6' 2" 198 lbs. born 25/ 6/1980 // # 18 Matthew Lloyd 6' 3" 198 lbs. born 16/ 4/1978 // # 4 Daniel McAlister 6' 1" 191 lbs. born 22/ 8/1978 // # 21 Dean Wallis 6' 3" 200 lbs. born 27/ 8/1969 // # 6 Sean Wellman 6' 4" 196 lbs. born 20/ 9/1974