#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int MAXLEN_FN = 30;
void OptionPrompt();
void Process(int *scoresArrayPtr, int numOfScores,
int& highestScore, int& lowestScore,
double& averageScore);
void CoutResults(int *scoresArrayPtr, int numOfScores,
int highestScore, int lowestScore,
double averageScore);
int main()
{
int *scoresArrayPtrHold = 0, *scoresArrayPtr = 0;
int arraySize = 0, numOfScores = 0, score,
lowestScore, highestScore;
double averageScore;
char inputFilename[MAXLEN_FN + 1],
reply;
ifstream fin;
ofstream fout;
cout << "I can process almost any # of scores for you" << endl;
OptionPrompt();
do
{
cin >> reply;
cin.ignore(999, '\n');
if (reply != '1' && reply != '2')
{
cout << "You have entered an invalid option" << endl;
cout << "Please re-enter your desired option" << endl;
OptionPrompt();
}
} while (reply != '1' && reply != '2');
if (reply == '1')
{
cout << "Enter the 1st score (any invalid score to quit): ";
cin >> score;
while (score >= 0 && score <= 100)
{
++numOfScores;
if (numOfScores > arraySize)
{
arraySize = int(1.5*arraySize + 1);
scoresArrayPtrHold = scoresArrayPtr;
scoresArrayPtr = new int [arraySize];
for (int i = 0; i < numOfScores - 1; ++i)
*(scoresArrayPtr + i) = *(scoresArrayPtrHold + i);
delete [] scoresArrayPtrHold;
scoresArrayPtrHold = 0;
}
*(scoresArrayPtr + numOfScores - 1) = score;
cout << "Enter the next score (any invalid score to end): ";
cin >> score;
}
if (numOfScores > 0)
{
Process(scoresArrayPtr, numOfScores, highestScore,
lowestScore, averageScore);
CoutResults(scoresArrayPtr, numOfScores, highestScore,
lowestScore, averageScore);
}
else
cout << "No scores to process..." << endl;
}
else
{
cout << "Enter name of input file (up to "
<< MAXLEN_FN << " characters): ";
int count = 0;
char oneChar;
oneChar = cin.get();
while (oneChar != '\n')
{
if (count < MAXLEN_FN)
{
inputFilename[count] = oneChar;
++count;
}
oneChar = cin.get();
}
inputFilename[count] = '\0';
fin.open(inputFilename, ios::in);
if (fin.fail())
{
cout << "Error opening file " << inputFilename << endl;
exit(EXIT_FAILURE);
}
fout.open("ASSIGN08.OUT", ios::out);
if (fout.fail())
{
cout << "Error opening file ASSIGN08.OUT" << endl;
fin.close();
exit(EXIT_FAILURE);
}
fin >> score;
while ( ! fin.eof() )
{
++numOfScores;
fin >> score;
}
if (numOfScores > 0)
{
fin.clear();
fin.seekg(0);
scoresArrayPtr = new int [numOfScores];
for (int i = 0; i < numOfScores; ++i)
fin >> *(scoresArrayPtr + i);
Process(scoresArrayPtr, numOfScores, highestScore,
lowestScore, averageScore);
CoutResults(scoresArrayPtr, numOfScores, highestScore,
lowestScore, averageScore);
fout << "Scores read: ";
for (int j = 0; j < numOfScores; ++j)
fout << *(scoresArrayPtr + j) << ' ';
fout << endl;
fout << "# of valid scores: " << numOfScores << endl;
fout << "Highest score: " << highestScore << endl;
fout << "Lowest score : " << lowestScore << endl;
fout << "Average score: "
<< setiosflags(ios::fixed | ios::showpoint)
<< setprecision(2) << averageScore << endl;
}
else
{
cout << "No scores to process..." << endl;
fout << "No scores to process..." << endl;
}
fin.close();
fout.close();
}
delete [] scoresArrayPtr;
scoresArrayPtr = 0;
return EXIT_SUCCESS;
}
void OptionPrompt()
{
cout << "You have the following options:" << endl;
cout << "1 Enter the scores interactively" << endl
<< " (results are output to screen only)" << endl;
cout << "2 Specify a file containing the scores" << endl
<< " (results are output to screen and file)" << endl
<< " (name of output file will be ASSIGN08.OUT)"
<< endl;
cout << "Your choice? ";
}
void Process(int *scoresArrayPtr, int numOfScores,
int& highestScore, int& lowestScore,
double& averageScore)
{
int sum = lowestScore = highestScore = *scoresArrayPtr;
for (int k = 1; k < numOfScores; ++k)
{
sum += *(scoresArrayPtr + k);
if (*(scoresArrayPtr + k) < lowestScore)
lowestScore = *(scoresArrayPtr + k);
else if (*(scoresArrayPtr + k) > highestScore)
highestScore = *(scoresArrayPtr + k);
}
averageScore = double(sum) / numOfScores;
}
void CoutResults(int *scoresArrayPtr, int numOfScores,
int highestScore, int lowestScore,
double averageScore)
{
cout << "Scores read: ";
for (int j = 0; j < numOfScores; ++j)
cout << *(scoresArrayPtr + j) << ' ';
cout << endl;
cout << "# of valid scores: " << numOfScores << endl;
cout << "Highest score: " << highestScore << endl;
cout << "Lowest score : " << lowestScore << endl;
cout << "Average score: "
<< setiosflags(ios::fixed | ios::showpoint)
<< setprecision(2) << averageScore << endl;
}