1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
//**************************************************
// Stephen Peterson
// Project 5: Lowest Score Drop
// this program will open a file named grades.txt,
// (a list of test grades) drop the lowest score,
// and calculate the average
//**************************************************
#include <iostream>
#include <fstream>
using namespace std;
// function prototypes
void getScore(int &scores, ifstream&);
void calcAverage(double &average);
int findLowest(int &min, ifstream&);
int main()
{
int scores = 0; //variable for each of the scores
int addScores = 0; //variable for accumulating the scores
int min = 0; //variable for the lowest score
int sumScores = 0; //variable for the total of the number of scores
int totalScores = 0; //variable for the total of the scores
double average = 0.0; //variable for the average of remaining scores
// open the file
ifstream inputFile;
inputFile.open("grades.txt");
// detect if open file failed
if (inputFile.fail())
{
cerr << "Error opening this file.\n\n";
return 10;
}
else
{
// describe the program to the user
cout << "This program will open a file named grades.txt\n"
<< "containing a list of grades. It will drop the\n"
<< "lowest grade then calculate and display the average.\n\n"
<< "\tPress the ENTER key to begin." << endl;
cin.get();
}
getScore(scores, inputFile);
calcAverage(average);
cout << "The average score in grades.txt is: " << average << endl;
return 0;
}
/****************************************************
** getScore will retrieve numbers from
** the grades.txt file and add them
*****************************************************/
void getScore(int &scores, ifstream& inputFile, int addScores, int sumScores)
{
while (inputFile >> addScores)
{
scores++;
sumScores += addScores;
}
}
/****************************************************
** calcAverage will calculate the average score
** from the grades.txt file minus the lowest score
*****************************************************/
void calcAverage(double &average, int scores, int sumScores, int totalScores, int min)
{
scores = 0;
findLowest(min, scores, sumScores);
average = (sumScores - min) / (totalScores - 1);
}
/****************************************************
** findLowest will identify the lowest score
** from the grades.txt file
*****************************************************/
int findLowest(int &min, int scores, int sumScores)
{
while (scores < sumScores)
{
if (scores < min)
{
min = scores;
scores++;
return min;
}
}
}
|