Advertisement
JangoBingoBango

Untitled

Apr 2nd, 2020
855
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.32 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. #include<iomanip>
  4.  
  5. #include<fstream>
  6.  
  7. #include<cstdlib>
  8.  
  9. #include<string>
  10.  
  11. #define MAX 100 // Maximum numbers in records
  12.  
  13. using namespace std;
  14.  
  15. // Defines class StudentGrade to store student information
  16.  
  17. class StudentGrade
  18.  
  19. {
  20.  
  21.     // Data member to store student information
  22.  
  23.     string firstName;
  24.  
  25.     string lastName;
  26.  
  27.     double testGrade[5];
  28.  
  29.     double average;
  30.  
  31.     double highest;
  32.  
  33.     char grade;
  34.  
  35. public:
  36.  
  37.     // Default constructor to assign default values to data member
  38.  
  39.     StudentGrade()
  40.  
  41.     {
  42.  
  43.         firstName = lastName = "";
  44.  
  45.         grade = ' ';
  46.  
  47.         average = 0.0;
  48.  
  49.         // Loops 5 time to assign parameter test score to 0
  50.  
  51.         for (int c = 0; c < 5; c++)
  52.  
  53.             testGrade[c] = 0;
  54.  
  55.     }// End of default constructor
  56.  
  57.     // Parameterized constructor to assign parameter values to data member
  58.  
  59.     StudentGrade(string fn, string ln, double tg[])
  60.  
  61.     {
  62.  
  63.         firstName = fn;
  64.  
  65.         lastName = ln;
  66.  
  67.         // Loops 5 time to assign parameter test score to data member test score
  68.  
  69.         for (int c = 0; c < 5; c++)
  70.  
  71.             testGrade[c] = tg[c];
  72.  
  73.     }// End of parameterized constructor
  74.  
  75.     // Prototype of functions
  76.  
  77.     void readFile(StudentGrade[], int&);
  78.  
  79.     void calculateGrade(StudentGrade[], int, double&);
  80.  
  81.     void showStudents(StudentGrade[], int, double);
  82.  
  83. };// End of function
  84.  
  85. // Function to read file contents and stores it in array of object
  86.  
  87. // Returns number of number read from file &len call by reference
  88.  
  89. void StudentGrade::readFile(StudentGrade sg[], int& len)
  90.  
  91. {
  92.  
  93.     string firstName;
  94.  
  95.     string lastName;
  96.  
  97.     double testGrade[5];
  98.  
  99.     // ifstream objects declared to read data from files
  100.  
  101.     ifstream fRead;
  102.  
  103.     // Opens the file for reading
  104.  
  105.     fRead.open("CSCI1306.txt");
  106.  
  107.     // Checks if the file unable to open for reading display's error message and stop
  108.  
  109.     if (!fRead)
  110.  
  111.     {
  112.  
  113.         cout << "\n ERROR: Unable to open the file for reading.";
  114.  
  115.         exit(0);
  116.  
  117.     }// End of if condition
  118.  
  119.     // Loops till end of the file
  120.  
  121.     while (!fRead.eof())
  122.  
  123.     {
  124.  
  125.         // Reads a record
  126.  
  127.         fRead >> firstName;
  128.  
  129.         fRead >> lastName;
  130.  
  131.         // Loops 5 time to read test score
  132.  
  133.         for (int c = 0; c < 5; c++)
  134.  
  135.             fRead >> testGrade[c];
  136.  
  137.         // Creates an object using parameterized constructor and assigns the object
  138.  
  139.         // at len index position
  140.  
  141.         sg[len] = StudentGrade(firstName, lastName, testGrade);
  142.  
  143.         // Increase the record counter by one
  144.  
  145.         len++;
  146.  
  147.     }// End of while loop
  148.  
  149.     // Close the file
  150.  
  151.     fRead.close();
  152.  
  153. }// End of function
  154.  
  155. // Function to calculate average, grade, highest grade
  156.  
  157. void StudentGrade::calculateGrade(StudentGrade sg[], int len, double& highestClass)
  158.  
  159. {
  160.  
  161.     // To store total
  162.  
  163.     double total;
  164.  
  165.     // Loops till number of students
  166.  
  167.     for (int c = 0; c < len; c++)
  168.  
  169.     {
  170.  
  171.         // Assigns current students first test score
  172.  
  173.         total = sg[c].testGrade[c];
  174.  
  175.         // Assigns current students first test score as highest score
  176.  
  177.         sg[c].highest = sg[c].testGrade[c];
  178.  
  179.         // Loops 5 times for test score
  180.  
  181.         for (int d = 1; d < 5; d++)
  182.  
  183.         {
  184.  
  185.             // Calculates total for current student
  186.  
  187.             total += sg[c].testGrade[d];
  188.  
  189.             // Checks if current students current test score is greater than
  190.  
  191.             // current student earlier highest score
  192.  
  193.             if (sg[c].testGrade[d] > sg[c].highest)
  194.  
  195.                 // Updates the current students highest score by assigning current students
  196.  
  197.                 // current test score
  198.  
  199.                 sg[c].highest = sg[c].testGrade[d];
  200.  
  201.         }// End of for loop for test score
  202.  
  203.         // Checks if current students highest score is greater than earlier class highest score
  204.  
  205.         if (sg[c].highest > highestClass)
  206.  
  207.             // Assigns current student highest score to class highest score
  208.  
  209.             highestClass = sg[c].highest;
  210.  
  211.         sg[c].average = total / 5.0;
  212.  
  213.     }// End of for loop for student
  214.  
  215.     // Loops till number of students
  216.  
  217.     for (int c = 0; c < len; c++)
  218.  
  219.         // Checks test score and assigns character grade
  220.  
  221.         if (sg[c].average >= 90)
  222.  
  223.             sg[c].grade = 'A';
  224.  
  225.         else if (sg[c].average >= 80)
  226.  
  227.             sg[c].grade = 'B';
  228.  
  229.         else if (sg[c].average >= 70)
  230.  
  231.             sg[c].grade = 'C';
  232.  
  233.         else if (sg[c].average >= 60)
  234.  
  235.             sg[c].grade = 'D';
  236.  
  237.         else
  238.  
  239.             sg[c].grade = 'F';
  240.  
  241. }// End of function
  242.  
  243. // Function to display the array
  244.  
  245. void StudentGrade::showStudents(StudentGrade sg[], int len, double highestClass)
  246.  
  247. {
  248.  
  249.     // Loops till number of students
  250.  
  251.     for (int c = 0; c < len; c++)
  252.  
  253.     {
  254.  
  255.         cout << endl << endl << left << " " << setw(15) << sg[c].firstName << setw(15) << sg[c].lastName;
  256.  
  257.         // Loops 5 times for test score
  258.  
  259.         for (int d = 0; d < 5; d++)
  260.  
  261.             cout << fixed << setprecision(2) << setw(7) << sg[c].testGrade[d];
  262.  
  263.         cout << setw(4) << sg[c].grade;
  264.  
  265.         cout << "\n Hist Test Grade: " << sg[c].highest;
  266.  
  267.     }// End of for loop
  268.  
  269.     cout << "\n\n\n Highest Test Grade in class: " << highestClass;
  270.  
  271. }// End of function
  272.  
  273. // main function definition
  274.  
  275. int main()
  276.  
  277. {
  278.  
  279.     // Creates an array of object of class StudentGrade of size MAX
  280.  
  281.     StudentGrade sg[MAX];
  282.  
  283.     // To store number of records
  284.  
  285.     int len = 0;
  286.  
  287.     // To store class highest
  288.  
  289.     double highestClass = 0;
  290.  
  291.     // Calls the function to read file contents
  292.  
  293.     sg[0].readFile(sg, len);
  294.  
  295.     // Calls the function to calculate average, grade, highest score, highest class
  296.  
  297.     sg[0].calculateGrade(sg, len, highestClass);
  298.  
  299.     // Calls the function to display result
  300.  
  301.     sg[0].showStudents(sg, len, highestClass);
  302.  
  303.     return 0;
  304.  
  305. }// End of main function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement