// Stephen van Someren Greve // CSCI 140 // 4/11/08 // // This is a program description. It does what it is programmed to do. #include #include #include using namespace std; // Player is a data type (a structure) that is an aggregate of all of the // significant aspects of a baseball player. For the purposes of this program, // there is very little data to manage: A name, times at bat, an number of // hits. struct Player { string _name; // Player's name (e.g., Bob) unsigned int _at_bat; // Times up at bat float _num_hits;// Number of successful hits }; const int NOTFOUND = -1; // Sentenal for FindPlayer istream& operator>>(istream& stream, Player& player); ostream& operator<<(ostream& stream, const Player& player); Player FindPlayer( Player team[], int player_ct, string Name); // main // This is the main body of the program. // TODO: Document me. int main() { // The team of players. Player team[93]; unsigned int team_size = 0; int menu; string Name, hit, match; int player_ct = 0; // This could be a separate function (e.g., read_team). ifstream fin("player.data"); Player p; fin >> p; while( fin.good() ) { team[team_size] = p; ++team_size; fin >> p; } int MENURETURN = -1; while(MENURETURN = -1); cout << "MENU\n" << "1) Report a player's batting average\n" << "2) Report all players' batting averages\n" << "3) Update a player's batting average\n" << "4) Save and quit\n" << "Choice:"; cin >> menu; if (menu == 1){ cout << "Which player?"; Player name; cin >> name; cout << name._num_hits/name._at_bat; MENURETURN = 1; } else if (menu == 2){ ifstream fin("player.data"); cout << "Player" << setw(15) << "Avg" << endl << "---------" << setw(15) << "----" << endl; while ( fin >> team[player_ct] ) { //team[player_ct]._name; cout << team[player_ct]._name << setw(15) << team[player_ct]._num_hits / team[player_ct]._at_bat << endl; ++player_ct; } MENURETURN = 1; } else if (menu == 3){ cout << "Which player? "; Player name; cin >> name; cout << "Did " << name._name << " get a hit? "; cin >> hit; if(hit.compare("yes") == 0){ //string.compare ++name._num_hits; ++name._at_bat; } if(hit == "no"){ ++name._at_bat; } else{ cout << "Please enter only yes or no.\n"; } MENURETURN = 1; } else if (menu == 4){ ofstream fout("players.data"); cout << "saving.."; fout << team[93]; cout << "saved\n"; } //goes to this else statement no matter what the menu choice was else{ cout << menu << " is not an option.\n"; } return 0; } // operator << // stream The output stream which will receive player's data. // player The player to write in to stream. // return The reference stream (for chaining inserts). // // This function will write the significant data contained in player to the // stream. ostream& operator<<(ostream& stream, const Player& player) { stream << player._name << player._at_bat << player._num_hits; return stream; } // operator >> // stream The output stream which will receive player's data. // player The player to write in to stream. // return The reference stream (for chaining inserts). // // This function will read the significant data contained in player to the // stream. istream& operator>>(istream& stream, Player& player) { stream >> player._name >> player._at_bat >> player._num_hits; return stream; } // FindPlayer // team All of the player. // player_ct The number of players in the team. // Name The name of the player to find. // This is how I'm going to find the player named Name. Player FindPlayer( Player team[], int player_ct, string Name) { ifstream fin("player.data"); while( fin.good()){ ++player_ct; if( team[player_ct]._name == Name){ cout << "Found Name!"; //this will only return some message when the player is not found } } return team[player_ct]; } //all the FindPlayer function is supposed to do is check to make sure that the player //entered is on the roster