Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<iomanip>
- #include<fstream>
- #include<cstdlib>
- #include<string>
- #define MAX 100 // Maximum numbers in records
- using namespace std;
- // Defines class StudentGrade to store student information
- class StudentGrade
- {
- // Data member to store student information
- string firstName;
- string lastName;
- double testGrade[5];
- double average;
- double highest;
- char grade;
- public:
- // Default constructor to assign default values to data member
- StudentGrade()
- {
- firstName = lastName = "";
- grade = ' ';
- average = 0.0;
- // Loops 5 time to assign parameter test score to 0
- for (int c = 0; c < 5; c++)
- testGrade[c] = 0;
- }// End of default constructor
- // Parameterized constructor to assign parameter values to data member
- StudentGrade(string fn, string ln, double tg[])
- {
- firstName = fn;
- lastName = ln;
- // Loops 5 time to assign parameter test score to data member test score
- for (int c = 0; c < 5; c++)
- testGrade[c] = tg[c];
- }// End of parameterized constructor
- // Prototype of functions
- void readFile(StudentGrade[], int&);
- void calculateGrade(StudentGrade[], int, double&);
- void showStudents(StudentGrade[], int, double);
- };// End of function
- // Function to read file contents and stores it in array of object
- // Returns number of number read from file &len call by reference
- void StudentGrade::readFile(StudentGrade sg[], int& len)
- {
- string firstName;
- string lastName;
- double testGrade[5];
- // ifstream objects declared to read data from files
- ifstream fRead;
- // Opens the file for reading
- fRead.open("CSCI1306.txt");
- // Checks if the file unable to open for reading display's error message and stop
- if (!fRead)
- {
- cout << "\n ERROR: Unable to open the file for reading.";
- exit(0);
- }// End of if condition
- // Loops till end of the file
- while (!fRead.eof())
- {
- // Reads a record
- fRead >> firstName;
- fRead >> lastName;
- // Loops 5 time to read test score
- for (int c = 0; c < 5; c++)
- fRead >> testGrade[c];
- // Creates an object using parameterized constructor and assigns the object
- // at len index position
- sg[len] = StudentGrade(firstName, lastName, testGrade);
- // Increase the record counter by one
- len++;
- }// End of while loop
- // Close the file
- fRead.close();
- }// End of function
- // Function to calculate average, grade, highest grade
- void StudentGrade::calculateGrade(StudentGrade sg[], int len, double& highestClass)
- {
- // To store total
- double total;
- // Loops till number of students
- for (int c = 0; c < len; c++)
- {
- // Assigns current students first test score
- total = sg[c].testGrade[c];
- // Assigns current students first test score as highest score
- sg[c].highest = sg[c].testGrade[c];
- // Loops 5 times for test score
- for (int d = 1; d < 5; d++)
- {
- // Calculates total for current student
- total += sg[c].testGrade[d];
- // Checks if current students current test score is greater than
- // current student earlier highest score
- if (sg[c].testGrade[d] > sg[c].highest)
- // Updates the current students highest score by assigning current students
- // current test score
- sg[c].highest = sg[c].testGrade[d];
- }// End of for loop for test score
- // Checks if current students highest score is greater than earlier class highest score
- if (sg[c].highest > highestClass)
- // Assigns current student highest score to class highest score
- highestClass = sg[c].highest;
- sg[c].average = total / 5.0;
- }// End of for loop for student
- // Loops till number of students
- for (int c = 0; c < len; c++)
- // Checks test score and assigns character grade
- if (sg[c].average >= 90)
- sg[c].grade = 'A';
- else if (sg[c].average >= 80)
- sg[c].grade = 'B';
- else if (sg[c].average >= 70)
- sg[c].grade = 'C';
- else if (sg[c].average >= 60)
- sg[c].grade = 'D';
- else
- sg[c].grade = 'F';
- }// End of function
- // Function to display the array
- void StudentGrade::showStudents(StudentGrade sg[], int len, double highestClass)
- {
- // Loops till number of students
- for (int c = 0; c < len; c++)
- {
- cout << endl << endl << left << " " << setw(15) << sg[c].firstName << setw(15) << sg[c].lastName;
- // Loops 5 times for test score
- for (int d = 0; d < 5; d++)
- cout << fixed << setprecision(2) << setw(7) << sg[c].testGrade[d];
- cout << setw(4) << sg[c].grade;
- cout << "\n Hist Test Grade: " << sg[c].highest;
- }// End of for loop
- cout << "\n\n\n Highest Test Grade in class: " << highestClass;
- }// End of function
- // main function definition
- int main()
- {
- // Creates an array of object of class StudentGrade of size MAX
- StudentGrade sg[MAX];
- // To store number of records
- int len = 0;
- // To store class highest
- double highestClass = 0;
- // Calls the function to read file contents
- sg[0].readFile(sg, len);
- // Calls the function to calculate average, grade, highest score, highest class
- sg[0].calculateGrade(sg, len, highestClass);
- // Calls the function to display result
- sg[0].showStudents(sg, len, highestClass);
- return 0;
- }// End of main function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement