Advertisement
Guest User

Untitled

a guest
Feb 12th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. #include <pch.h>
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. const int NO_OF_STUDENTS = 20;
  10.  
  11. struct studentType
  12. {
  13. string studentFName;
  14. string studentLName;
  15. int testScore;
  16. char grade;
  17. };
  18.  
  19. void getData(ifstream& inFile, studentType sList[], int listSize);
  20. void calculateGrade(studentType sList[], int listSize);
  21. int highestScore(const studentType sList[], int listSize);
  22. void printResult(ofstream& outFile, const studentType sList[], int listSize);
  23.  
  24. int main()
  25. {
  26. ifstream inData;
  27. ofstream outData;
  28. studentType studentList[NO_OF_STUDENTS];
  29.  
  30. // Open input file
  31. inData.open("Ch9_Ex2Data.txt");
  32. if (!inData)
  33. {
  34. cout << "The input file does not exist. Program terminates!"
  35. << endl;
  36. return 1;
  37. }
  38.  
  39. // Open output file
  40. outData.open("Ch9_Ex2Out.txt");
  41. if (!outData)
  42. {
  43. cout << "Cannot open the output file. Program terminates!"
  44. << endl;
  45. return 1;
  46. }
  47.  
  48. // TODO
  49. getData(inData, studentList, 20);
  50. calculateGrade(studentList, 20);
  51. printResult(outData, studentList, 20);
  52. return 0;
  53. }
  54.  
  55. void getData(ifstream& inFile, studentType sList[], int listSize)
  56. {
  57. for (int i = 0; i < listSize; i++)
  58. inFile >> sList[i].studentFName >> sList[i].studentLName
  59. >> sList[i].testScore;
  60. }
  61.  
  62. void calculateGrade(studentType sList[], int listSize)
  63. {
  64. //Done
  65. {
  66.  
  67. for (int i = 0;i < NO_OF_STUDENTS; i++)
  68. if (sList[i].testScore < 60)
  69. sList[i].grade = 'F';
  70. else if (sList[i].testScore < 70)
  71. sList[i].grade = 'D';
  72. else if (sList[i].testScore < 80)
  73. sList[i].grade = 'C';
  74. else if (sList[i].testScore < 90)
  75. sList[i].grade = 'B';
  76. else
  77. sList[i].grade = 'A';
  78. }
  79.  
  80. }
  81. int highestScore(const studentType sList[], int listSize)
  82. {
  83. int hScore = sList[0].testScore;
  84.  
  85. // TODO
  86. for (int i = 0;i < 20;i++)
  87. {
  88. if (hScore < sList[i].testScore)
  89. hScore = sList[i].testScore;
  90. }
  91.  
  92. return hScore;
  93. }
  94.  
  95. void printResult(ofstream& outFile, const studentType sList[], int listSize)
  96. {
  97. cout << left << setw(30) << "Student Name" << right << setw(10) << "TestScore" << right << setw(7) << "Grade" << endl;
  98. string name;
  99. int high, i;
  100.  
  101. for (i = 0; i < listSize; i++)
  102. {
  103. name = sList[i].studentLName + ", " + sList[i].studentFName;
  104. cout << left << setw(30) << name << right << setw(10) << sList[i].testScore << right << setw(7) << sList[i].grade << endl;
  105.  
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement