SOURCE FILE: sampProg.cpp
#include <fstream>
#include <iostream>
#include <iomanip>
#include <assert.h>
using namespace std ;
const int MAX_NUM_PLAYERS = 100; // maximum number of players
/* Program to read golf scores from the file "scores" (no white space
after last score) and then to print those scores
*/
void ReadScoresFile(int [], int&); // prototypes
void DisplayScores(const int [], int);
int main(void)
{
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: "scores" 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("scores");
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;
}