Advertisement
Sirallens

Untitled

May 2nd, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. //******************************************************************************
  2. // CSCI 1380.02 Spring 2016 Hw # 3
  3. // Hideki Garcia
  4. //
  5. // Simple program to practice data extraction from a file using the Standard Library FSTREAM
  6. //
  7. //******************************************************************************
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11. #include <string>
  12. #include <iomanip>
  13. using namespace std;
  14.  
  15. const int MAX = 600;
  16.  
  17. int average(int sum, int quantity);
  18.  
  19. int main()
  20. {
  21.     string name[MAX] = { " " };
  22.     int grade[MAX] = { 0 };
  23.     int quantity = 0;
  24.     int x = 0;
  25.     double avg = 0;
  26.     int sum = 0;
  27.     string n = " ";
  28.     ifstream inFile;
  29.    
  30.     inFile.open("indata3.txt");
  31.     if (!inFile.is_open())
  32.     {
  33.         cout << "Could not open file myfile.txt." << endl;
  34.         return 1; // 1 indicates error
  35.     }
  36.  
  37.     while(!inFile.eof())
  38.     {
  39.         inFile >> name[x];
  40.         inFile >> grade[x];
  41.         x++;
  42.     }
  43.  
  44.     inFile.close();
  45.     cout << "Enter quantity of grades to be processed (" << x << "): " << endl;
  46.     cin >> quantity;
  47.  
  48.     for (x = 0; x < quantity; x++)
  49.     {
  50.         sum = sum + grade[x];
  51.     }
  52.  
  53.     avg = average(sum, quantity);
  54.  
  55.     cout << "Average grade: " << avg << endl;
  56.    
  57.  
  58.     for (x = 0; x<quantity; x++)
  59.     {
  60.         cout << "Name: " << name[x]  << "  Grade: " << grade[x] << "  Comments: ";
  61.         if (grade[x]<avg)
  62.         {
  63.             cout << " below average" << endl;
  64.         }
  65.         else if (grade[x] > avg) {
  66.             cout << " above average" << endl;
  67.         }
  68.         else {
  69.             cout << " average" << endl;
  70.         }
  71.     }
  72.  
  73.     system("pause");
  74.     return 0;
  75. }
  76.  
  77. int average(int sum, int quantity)
  78. {
  79.     double avg = 0;
  80.     avg = static_cast<double>(sum) / static_cast<double>(quantity);
  81.     avg = avg + .5;
  82.     return static_cast<int>(avg);
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement