Advertisement
PuffySheep

College Averages

Oct 26th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int main()
  11. {
  12.     //Define the Variables
  13.     string Firstname, Gender, College, path;
  14.     string filename = "scores.txt";
  15.    
  16.     float Average = 0, Score = 0, TotalUN = 0, CountUN = 0, TotalCC = 0, CountCC = 0, TotalM = 0, CountM = 0, TotalF = 0, CountF = 0;
  17.     fstream infile;
  18.    
  19.  
  20.     cout << "Please input the path to the file scores.txt: ";
  21.     cin  >> path;
  22.  
  23.     string fullpath = path + filename;
  24.  
  25.     //open the file
  26.     infile.open(fullpath);
  27.  
  28.     //If an error occurs
  29.     if (!infile)
  30.     {
  31.         cout << "Cannot open the file at the target location."
  32.              << "The program will now terminate" << endl;
  33.         exit;
  34.     }
  35.  
  36.     cout << "This is the data within the specified file: " << endl;
  37.     while (!infile.eof())
  38.     {
  39.  
  40.         infile >> Firstname >> Gender >> College >> Score;
  41.  
  42.         cout << fixed << showpoint << setprecision(2);
  43.         cout << setw(10) << left << Firstname
  44.              << setw(10) << left << Gender
  45.              << setw(10) << left << College
  46.              << setw(8) << Score << endl;
  47.     }
  48.    
  49.     cout << "Here are the averages: " << endl;
  50.  
  51.     if (College == "UN")
  52.     {
  53.         TotalUN = TotalUN + Score;
  54.         CountUN++;
  55.     }
  56.  
  57.     cout << fixed << showpoint << setprecision(2);
  58.     cout << "Average of University students = " << TotalUN / CountUN << endl;
  59.  
  60.     if (College == "CC")
  61.     {
  62.         TotalCC = TotalCC + Score;
  63.         CountCC++;
  64.     }
  65.  
  66.     cout << fixed << showpoint << setprecision(2);
  67.     cout << "Average of Community College students = " << TotalCC / CountCC << endl;
  68.  
  69.     if (Gender == "M")
  70.     {
  71.         TotalM = TotalM + Score;
  72.         CountM++;
  73.     }
  74.  
  75.     cout << fixed << showpoint << setprecision(2);
  76.     cout << "Average of Male students = " << TotalM / CountM << endl;
  77.  
  78.     if (Gender == "F")
  79.     {
  80.         TotalF = TotalF + Score;
  81.         CountF++;
  82.     }
  83.  
  84.     cout << fixed << showpoint << setprecision(2);
  85.     cout << "Average of Female students = " << TotalF / CountF << endl;
  86.  
  87.     infile.close();
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement