Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "genlib.h"
- #include "simpio.h"
- #include <iostream>
- #include <conio.h>
- #include <fstream>
- #include <sstream>
- ifstream in;
- struct gradeList{
- int numGrades;
- int min;
- int max;
- double avg;
- };
- gradeList statTaker(ifstream &in, gradeList gList)
- {
- gradeList outputList;
- int numGrades = 0;
- int gradeSum = 0;
- int maxTillNow = 0;
- int minTillNow = 100;
- while (!in.fail())
- {
- in.clear();
- string line;
- getline(in,line);
- cout << line << endl;
- stringstream ss(line);
- int thisGrade;
- ss >> thisGrade;//get integer representation of this grade
- gradeSum += thisGrade;//add grade to the running total for average
- numGrades++;//iterate count number of grades
- if (thisGrade > maxTillNow)
- maxTillNow = thisGrade;//set max
- if (thisGrade < minTillNow)
- minTillNow = thisGrade;//set min
- }
- double gradeAverage = double(gradeSum)/numGrades;
- outputList.numGrades = numGrades;
- outputList.min = minTillNow;
- outputList.max = maxTillNow;
- outputList.avg = gradeAverage;
- in.clear();
- return outputList;
- }
- int main()
- {
- cout << "Enter score file name: ";
- string s = GetLine();
- in.open(s.c_str());
- while (in.fail())
- {
- in.clear();
- cout << "Invalid file. Enter file name: ";
- s = GetLine();
- in.open(s.c_str());
- }
- gradeList gList1;
- gradeList stats = statTaker(in, gList1);
- cout << "The average is " << stats.avg << endl << "The lowest score is " << stats.min << endl << "The highest score is " << stats.max;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment