Advertisement
Koalaazz

Student Grades

Feb 17th, 2021
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <conio.h>
  4. #include <string>
  5. using namespace std;
  6.  
  7. struct studentType
  8. {
  9. string studentFname;
  10. string studentLname;
  11. int testScore;
  12. char grade;
  13. } student[20];
  14. void OpenFile ()
  15. {
  16. studentType StudentList[20];
  17. ifstream structFile; //opens struct file
  18. structFile.open("struct.txt");
  19. if (structFile.is_open())
  20. {
  21. cout << "Student File Opened! ";
  22. }
  23. else cout << "Unable to open struct file. The location may have changed or the file may be damaged.";
  24. }
  25. void assign()
  26. {
  27. string studentInfo;
  28. ifstream file("struct.txt"); //assigns grades to students
  29. if (file.is_open())
  30. {
  31. for (int i = 0; i < 20; i++)
  32. {
  33. getline(file, studentInfo, ' ');
  34. student[i].studentFname = studentInfo;
  35. getline(file, studentInfo, ',');
  36. student[i].studentLname = studentInfo;
  37. getline(file, studentInfo, '\n');
  38. student[i].testScore = stoi(studentInfo);
  39.  
  40. }
  41. }
  42. else cout << "Unable to open struct file. The location may have changed or the file may be damaged.";
  43. cout << "Grades Assigned! "; //displays only if grades are assigned to students
  44. }
  45. void grade()
  46. {
  47. for (int i = 0; i < 20; i++) //assignes letter grades to number
  48. {
  49. if (student[i].testScore < 60)
  50. {
  51. student[i].grade = 'F'; //bad grade
  52. }
  53. else if (student[i].testScore < 70)
  54. {
  55. student[i].grade = 'D';
  56. }
  57. else if (student[i].testScore < 80)
  58. {
  59. student[i].grade = 'C';
  60. }
  61. else if (student[i].testScore < 90)
  62. {
  63. student[i].grade = 'B';
  64. }
  65. else if (student[i].grade = 'A');
  66. }
  67. cout << "Letters assigned!\n\n"; //displays only if letters are assigned to grade
  68. }
  69. void DisplayStudents()
  70. {
  71. cout << "Last Name, First Name\t Grade" << "\n";
  72. cout << "\nStudents: \n";
  73. int highest = 0;
  74. int spaces;
  75. for (int i = 0; i < 20; i++)
  76. {
  77. if (student[i].studentFname.size() + student[i].studentLname.size() > highest)
  78. {
  79. highest = student[i].studentFname.size() + student[i].studentLname.size();
  80. }
  81. }
  82. for (int i = 0; i < 20; i++)
  83. {
  84. spaces = (highest - (student[i].studentFname.size() + student[i].studentLname.size()) + 2);
  85. cout << "\n" << student[i].studentLname << ", " << student[i].studentFname;
  86. for (int i = 0; i < spaces; i++)
  87. {
  88. cout << " ";
  89. }
  90. cout << student[i].grade << " - " << student[i].testScore;
  91. }
  92. cout << "\n\n";
  93. }
  94. int main() //man function
  95. {
  96. OpenFile();
  97. assign();
  98. grade();
  99. DisplayStudents();
  100. system("PAUSE");
  101. return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement