Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. /**
  2. Name: Jesse M. Bevil
  3. StudentID: L20372072
  4. Class: 2017_Fall_COSC_3306_48F_1 - UNIXC++
  5. Instructor: Dr. Sujing Wang
  6. Date: Sunday, November 14th, 2017
  7. Due: Sunday, December 3rd, 2017
  8. Programming Language: C++
  9. Class Name: studentsFromFile
  10. Includes:
  11. iostream
  12. cstring
  13. Namespace: std
  14. Package: default
  15. Functions:
  16. double getAvg(double, double, double); //used to get average of three student grades
  17. double getClassAvg(double); //used to get entire classes average based on total studentCount
  18.  
  19.  
  20. Inheritance: None
  21. 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,
  22. 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
  23. a file (in a special format).
  24.  
  25. Bugs: None that I'm aware of.
  26. Incomplete portions of the program: None that I am aware of, or I'd have done them of course.
  27.  
  28. ****Notes: input file should be placed in root directory. Output file will be generated in root directory. I will include a sample studentInfo.txt
  29. and a sample formattedStudentsInfo.txt from my own personal output. ****
  30. **/
  31.  
  32. #include <iostream>
  33. #include <fstream>
  34. #include <string>
  35. #include <sstream>
  36. #include <cmath>
  37. using namespace std;
  38.  
  39. //Global variables
  40. int studentCount = 0; //needed to be global for access by function getClassAvg
  41. int debug = false; //if set to true will output additional information to the console on run.
  42. //prototypes
  43. double getAvg(double, double, double); //used to get average of three student grades
  44. double getClassAvg(double); //used to get entire classes average based on total studentCount
  45.  
  46. int main()
  47. {
  48. //define variables for use in program
  49. double gpa, totalAvg = 0, firstGradeAvg = 0, secondGradeAvg = 0, thirdGradeAvg = 0;
  50. string firstName, lastName, studentID;
  51. double firstGrade, secondGrade, thirdGrade;
  52. //open streams
  53. ifstream inStream ("input.dat");
  54. ofstream outStream ("formattedStudentInfo.txt");
  55. outStream.setf(ios::fixed | ios::showpoint | ios::left); //set fixed flags | shows decimal | justification left
  56. outStream.precision(2); //shows no more than 2 decimal points.
  57.  
  58. //do this as long as we haven't reached the end of the file
  59. while (inStream >> firstName)
  60. {
  61. inStream >> lastName >> studentID >> firstGrade >> secondGrade >> thirdGrade;
  62. gpa = getAvg(firstGrade, secondGrade, thirdGrade);
  63. totalAvg += gpa;
  64. firstGradeAvg += firstGrade; //adds every students grade for the first test to this value
  65. secondGradeAvg += secondGrade; //adds every students grade for the second test to this value
  66. thirdGradeAvg += thirdGrade; //adds every students grade for the third test to this value
  67. studentCount++; //counts the students
  68.  
  69. //debug output to console, only runs if debug == true;
  70. if (debug)
  71. {
  72. cout << firstName << lastName << endl;
  73. cout << "firstGrade: " << firstGrade << endl;
  74. cout << "secondGrade: " << secondGrade << endl;
  75. cout << "thirdGrade: " << thirdGrade << endl << endl;
  76. cout << "firstGradeAvg: " << firstGradeAvg << endl;
  77. cout << "secondGradeAvg: " << secondGradeAvg << endl;
  78. cout << "thirdGradeAvg: " << thirdGradeAvg << endl << endl;
  79. }
  80. //output per student with their 3 test averages
  81. outStream.setf(ios::left);//justification left
  82. outStream << lastName << ", " << firstName[0] << ". " << studentID << " ";
  83. outStream.setf(ios::right); //justification right
  84. outStream.width(30);
  85. outStream << gpa << "\n";
  86. }
  87. //output Class Average, Tests, Test 1-3 averages to the file. Averages are
  88. outStream << "\n" << "Class Average: ";
  89. outStream.setf(ios::right); //justification right
  90. outStream.width(23);
  91. outStream << getClassAvg(totalAvg) << "\n" << "\n" << "TESTS" << "\n" << "Test 1: ";
  92. outStream.setf(ios::right); //justification right
  93. outStream.width(30);
  94. outStream << getClassAvg(firstGradeAvg) << "\n";
  95. outStream << "Test 2: ";
  96. outStream.setf(ios::right); //justification right
  97. outStream.width(30);
  98. outStream << getClassAvg(secondGradeAvg) << "\n";
  99. outStream << "Test 3: ";
  100. outStream.setf(ios::right); //justification right
  101. outStream.width(30);
  102. outStream << getClassAvg(thirdGradeAvg) << "\n";
  103.  
  104. //This last output was at the bottom of the output format as Tests TestsAverage, it seems redundant as this will match Class average.
  105. outStream << "Average of Test Averages: ";
  106. outStream.setf(ios::right); //justification right
  107. outStream.width(12);
  108. outStream << getAvg(getClassAvg(firstGradeAvg), getClassAvg(secondGradeAvg), getClassAvg(thirdGradeAvg)) << "\n";
  109.  
  110. //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
  111. inStream.close();
  112. outStream.close();
  113.  
  114. //only runs if debug == true;
  115. if (debug)
  116. {
  117. //output to console
  118. cout << "Total Students processed: " << studentCount << endl;
  119.  
  120. //used to pause the console (Primarily for use in Visual Studios but functions in unix g++ as well.
  121. cout << "Press any key to continue" << endl;
  122. cin.get();
  123. }
  124. }
  125.  
  126. //Returns average of 3 tests
  127. double getAvg(double a, double b, double c)
  128. {
  129. return (a + b + c) / 3;
  130. }
  131.  
  132. //returns a double average of a total accumulated grade across all students divided by studentCount
  133. double getClassAvg(double a)
  134. {
  135. return a / studentCount;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement