// Name: John H. Hacker // Assg: Program #3 - Play Ball // Date: June 27, 2006 // Desc: This program simulates calculating some statistics for // a baseball team using a 2-dimensional array of statistics. #include #include #include using namespace std; const int MAX_NUM_PLAYERS = 25; const int NUM_STATS = 3; // Prototypes for functions used in the PlayBall program int input(string filename, string players[], int stats[MAX_NUM_PLAYERS][NUM_STATS]); // The main function first reads in the data from the input, then // causes the data to be sorted by the radixsort() function, and // displays the final sorted results on standard output. int main() { int numPlayers; string players[MAX_NUM_PLAYERS]; int stats[MAX_NUM_PLAYERS][NUM_STATS]; numPlayers = input("playerstats.dat", players, stats); } // input // // Input integers from a file. The user supplies the name of the file // to read from, and an array to fill up with the integers. This // function returns the number of items that were read from the file. // // Params: // filename a string that is the name of the file // to be open for input // players an array of strings, we will read this in first from // the input file // stats a 2-dimensional array that holds the player stats, // each row holds stats for a single player, and the // columns 0,1,2 hold stats for the players hits, walks // and outs respectively // Result: // numPlayers this function returns the number of players on the // team and in the stats array that was read from the // input file int input(string filename, string players[], int stats[MAX_NUM_PLAYERS][NUM_STATS]) { int numPlayers = 0; ifstream infile; // first open the file and check that it opened properly infile.open(filename.c_str()); if (!infile) { cerr << "Error, could not open file: " << filename << endl << " Perhaps the file is not in your build directory?" << endl; exit(0); } // The first N lines contain player's names, one name on each line. // The file contains a sentenial value "STATS" to indicate when // the player names are done and their stats are going to begin. // So this first loop simply reads in the player's names into the // players array. string playerName = ""; while (true) { getline(infile, playerName); if (playerName == "STATS") break; players[numPlayers] = playerName; numPlayers++; } // Now we read in the stats, there should be numPlayers lines // left in the file at this point. Each line contains 3 integers, // the number of hits, walks and outs respectively for a player. int player; int hits, walks, outs; for (player=0; player> hits >> walks >> outs; stats[player][0] = hits; stats[player][1] = walks; stats[player][2] = outs; } // always good to explicitly close a file when we are done with it infile.close(); return numPlayers; }