Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- Name: Jesse M. Bevil
- StudentID: L20372072
- Class: 2017_Fall_COSC_3306_48F_1 - UNIXC++
- Instructor: Dr. Sujing Wang
- Date: Sunday, November 14th, 2017
- Due: Sunday, December 3rd, 2017
- Programming Language: C++
- Class Name: studentsFromFile
- Includes:
- iostream
- cstring
- Namespace: std
- Package: default
- Functions:
- double getAvg(double, double, double); //used to get average of three student grades
- double getClassAvg(double); //used to get entire classes average based on total studentCount
- Inheritance: None
- Purpose: In response to Module 5 Question 1: Write a program that reads from a formatted file a list of 6 students and their grades for 3tests,
- computes the average test score for each student and the class and the overall average for each test and for all tests and outputs them in
- a file (in a special format).
- Bugs: None that I'm aware of.
- Incomplete portions of the program: None that I am aware of, or I'd have done them of course.
- ****Notes: input file should be placed in root directory. Output file will be generated in root directory. I will include a sample studentInfo.txt
- and a sample formattedStudentsInfo.txt from my own personal output. ****
- **/
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- #include <cmath>
- using namespace std;
- //Global variables
- int studentCount = 0; //needed to be global for access by function getClassAvg
- int debug = false; //if set to true will output additional information to the console on run.
- //prototypes
- double getAvg(double, double, double); //used to get average of three student grades
- double getClassAvg(double); //used to get entire classes average based on total studentCount
- int main()
- {
- //define variables for use in program
- double gpa, totalAvg = 0, firstGradeAvg = 0, secondGradeAvg = 0, thirdGradeAvg = 0;
- string firstName, lastName, studentID;
- double firstGrade, secondGrade, thirdGrade;
- //open streams
- ifstream inStream ("input.dat");
- ofstream outStream ("formattedStudentInfo.txt");
- outStream.setf(ios::fixed | ios::showpoint | ios::left); //set fixed flags | shows decimal | justification left
- outStream.precision(2); //shows no more than 2 decimal points.
- //do this as long as we haven't reached the end of the file
- while (inStream >> firstName)
- {
- inStream >> lastName >> studentID >> firstGrade >> secondGrade >> thirdGrade;
- gpa = getAvg(firstGrade, secondGrade, thirdGrade);
- totalAvg += gpa;
- firstGradeAvg += firstGrade; //adds every students grade for the first test to this value
- secondGradeAvg += secondGrade; //adds every students grade for the second test to this value
- thirdGradeAvg += thirdGrade; //adds every students grade for the third test to this value
- studentCount++; //counts the students
- //debug output to console, only runs if debug == true;
- if (debug)
- {
- cout << firstName << lastName << endl;
- cout << "firstGrade: " << firstGrade << endl;
- cout << "secondGrade: " << secondGrade << endl;
- cout << "thirdGrade: " << thirdGrade << endl << endl;
- cout << "firstGradeAvg: " << firstGradeAvg << endl;
- cout << "secondGradeAvg: " << secondGradeAvg << endl;
- cout << "thirdGradeAvg: " << thirdGradeAvg << endl << endl;
- }
- //output per student with their 3 test averages
- outStream.setf(ios::left);//justification left
- outStream << lastName << ", " << firstName[0] << ". " << studentID << " ";
- outStream.setf(ios::right); //justification right
- outStream.width(30);
- outStream << gpa << "\n";
- }
- //output Class Average, Tests, Test 1-3 averages to the file. Averages are
- outStream << "\n" << "Class Average: ";
- outStream.setf(ios::right); //justification right
- outStream.width(23);
- outStream << getClassAvg(totalAvg) << "\n" << "\n" << "TESTS" << "\n" << "Test 1: ";
- outStream.setf(ios::right); //justification right
- outStream.width(30);
- outStream << getClassAvg(firstGradeAvg) << "\n";
- outStream << "Test 2: ";
- outStream.setf(ios::right); //justification right
- outStream.width(30);
- outStream << getClassAvg(secondGradeAvg) << "\n";
- outStream << "Test 3: ";
- outStream.setf(ios::right); //justification right
- outStream.width(30);
- outStream << getClassAvg(thirdGradeAvg) << "\n";
- //This last output was at the bottom of the output format as Tests TestsAverage, it seems redundant as this will match Class average.
- outStream << "Average of Test Averages: ";
- outStream.setf(ios::right); //justification right
- outStream.width(12);
- outStream << getAvg(getClassAvg(firstGradeAvg), getClassAvg(secondGradeAvg), getClassAvg(thirdGradeAvg)) << "\n";
- //close my streams (would be done automatically at the end of main, but doing it as a matter of habit to avoid unclosed streams later
- inStream.close();
- outStream.close();
- //only runs if debug == true;
- if (debug)
- {
- //output to console
- cout << "Total Students processed: " << studentCount << endl;
- //used to pause the console (Primarily for use in Visual Studios but functions in unix g++ as well.
- cout << "Press any key to continue" << endl;
- cin.get();
- }
- }
- //Returns average of 3 tests
- double getAvg(double a, double b, double c)
- {
- return (a + b + c) / 3;
- }
- //returns a double average of a total accumulated grade across all students divided by studentCount
- double getClassAvg(double a)
- {
- return a / studentCount;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement