Guest User

Untitled

a guest
Mar 29th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include "genlib.h"
  2. #include "simpio.h"
  3. #include <iostream>
  4. #include <conio.h>
  5. #include <fstream>
  6. #include <sstream>
  7.  
  8. ifstream in;
  9. struct gradeList{
  10.     int numGrades;
  11.     int min;
  12.     int max;
  13.     double avg;
  14. };
  15.  
  16. gradeList statTaker(ifstream &in, gradeList gList)
  17. {
  18.     gradeList outputList;
  19.     int numGrades = 0;
  20.     int gradeSum = 0;
  21.     int maxTillNow = 0;
  22.     int minTillNow = 100;
  23.     while (!in.fail())
  24.     {
  25.         in.clear();
  26.         string line;
  27.         getline(in,line);
  28.         cout << line << endl;
  29.         stringstream ss(line);
  30.         int thisGrade;
  31.         ss >> thisGrade;//get integer representation of this grade
  32.         gradeSum += thisGrade;//add grade to the running total for average
  33.         numGrades++;//iterate count number of grades
  34.         if (thisGrade > maxTillNow)
  35.             maxTillNow = thisGrade;//set max
  36.         if (thisGrade < minTillNow)
  37.             minTillNow = thisGrade;//set min
  38.     }
  39.     double gradeAverage = double(gradeSum)/numGrades;
  40.     outputList.numGrades = numGrades;
  41.     outputList.min = minTillNow;
  42.     outputList.max = maxTillNow;
  43.     outputList.avg = gradeAverage;
  44.     in.clear();
  45.     return outputList;
  46. }
  47.  
  48. int main()
  49. {
  50.     cout << "Enter score file name: ";
  51.     string s = GetLine();
  52.     in.open(s.c_str());
  53.     while (in.fail())
  54.     {
  55.         in.clear();
  56.         cout << "Invalid file. Enter file name: ";
  57.         s = GetLine();
  58.         in.open(s.c_str());
  59.     }
  60.     gradeList gList1;
  61.     gradeList stats = statTaker(in, gList1);
  62.     cout << "The average is " << stats.avg << endl << "The lowest score is " << stats.min << endl << "The highest score is " << stats.max;
  63.  
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment