SOURCE FILE: ex1003.cpp
//
// Example 10.3. Program to read golf scores from the file "ex1003.dat"
// (no white space after last score) and then to print those scores
//
#include <fstream.h>
#include <iomanip.h>
#include <assert.h>
const int MAX_NUM_PLAYERS = 100; // maximum number of players
int main(void)
{
void ReadScoresFile(int [], int&); // prototypes
void DisplayScores(const int [], int);
int TourneyScore[MAX_NUM_PLAYERS], // array of scores
TourneyNum; // actual number of players
ReadScoresFile(TourneyScore, TourneyNum);
DisplayScores(TourneyScore, TourneyNum);
return 0;
}
//
// Function to read values for an array
// Pre: ex1003.dat is a file of golf scores.
// Post: score is a constant integer array of golf scores.
// NumPlayers is the number of golf scores.
//
void ReadScoresFile(int score[], int& NumPlayers)
{
ifstream GolfFile("ex1003.dat");
assert(GolfFile);
// read values for array score until end of file
NumPlayers = 0;
while ((NumPlayers < MAX_NUM_PLAYERS) &&
(GolfFile >> score[NumPlayers]))
NumPlayers++;
// print error message if too much data for array
if (!GolfFile.eof())
cout << "\nFile contained more than " << MAX_NUM_PLAYERS
<< " scores.\n"
<< "Only the first " << MAX_NUM_PLAYERS
<< " scores will be processed.\n";
GolfFile.close();
}
//
// Function to display an array of golf scores
// Pre: score is an integer array of golf scores.
// NumPlayers is the number of golf scores.
// Post: The scores from score have been displayed.
//
void DisplayScores(const int score[], int NumPlayers)
{
cout << "\n\tGolf Scores for Round:\n\n";
cout << "\tPlayer #\tScore\n";
cout << "\t________\t_____\n";
for (int player = 0; player < NumPlayers; player++)
cout << "\t" << setw(4) << player + 1
<< "\t\t" << setw(4) << score[player] << endl;
}