// // Example 10.2. Program to read golf scores until the user // types a trailer value and then to print those scores // #include #include int main(void) { const int MAX_NUM_PLAYERS = 100; // maximum number of players const int TRAILER = -9999; // trailer for input int score[MAX_NUM_PLAYERS], // array of scores player, // index for array NumPlayers, // actual number of players OneScore; // used to read a score // read values for array score until trailer cout << "\nPlease enter up to " << MAX_NUM_PLAYERS << " scores. " << "Enter " << TRAILER << " to signal end of data.\n"; player = 0; cin >> OneScore; while (player < MAX_NUM_PLAYERS && OneScore != TRAILER) { score[player] = OneScore; player++; cin >> OneScore; } // print error message if too much data for array if (player == MAX_NUM_PLAYERS && OneScore != TRAILER) cout << "\nYou have entered more than " << MAX_NUM_PLAYERS << " scores.\n" << "Only the first " << MAX_NUM_PLAYERS << " scores will be processed.\n"; // last value of player is actual number of scores NumPlayers = player; // Print the scores cout << "\n\tGolf Scores for Round:\n\n"; cout << "\tPlayer #\tScore\n"; cout << "\t________\t_____\n"; for (player = 0; player < NumPlayers; player++) cout << "\t" << setw(4) << player + 1 << "\t\t" << setw(4) << score[player] << endl; return 0; }