SOURCE FILE: ex1001.cpp


//   
// Example 10.1.  Program to read the number of golf scores   
// and then to read and print those scores   
//    
   
#include <iostream.h>
#include <iomanip.h>
   
const int MAX_NUM_PLAYERS = 100;   
   
int main(void)   
{   
   int GetNumPlayers(void);   
      
   int score[MAX_NUM_PLAYERS],  // array of scores 
       player,                  // index for array 
       NumPlayers;              // actual number of players 
         
   NumPlayers = GetNumPlayers();    
         
   // read values for array score 
   cout << "\nPlease enter " << NumPlayers << " scores "
        << "with blanks separating scores:\n";
   
   for (player = 0; player < NumPlayers; player++)
      cin >> score[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;
}   
   
//   
// Function to read and return number of players   
// Pre:  none   
// Post: The number of players, an integer between   
//       1 and MAX_NUM_PLAYERS, was returned.   
//    
   
int GetNumPlayers(void)   
{   
   int NumPlayers;      // actual number of players 
      
   cout << "How many players are in the golf tournament? ";
   cin  >> NumPlayers;
      
   while ((NumPlayers <= 0) || (MAX_NUM_PLAYERS < NumPlayers))   
   {   
      cout << "The number of players must be between 1 and "    
           << MAX_NUM_PLAYERS << endl;
      cout << "Please enter the number of players again.  ";
      cin  >> NumPlayers;
   }   
      
   return NumPlayers;   
}