Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <string>
  5. #include <ctype.h>
  6.  
  7. int main() {
  8. unsigned int num_students {0};
  9. unsigned int class_sum {0};
  10. std::string answers {""};
  11. unsigned int line_number {0};
  12.  
  13. std::cout << "Student Score" << std::endl;
  14. std::cout << "--------------------------" << std::endl;
  15.  
  16. std::ifstream file;
  17. file.open("data/students.txt"); // pseudo path
  18.  
  19. if (!file || !file.is_open()) {
  20. std::cerr << "Error: There was a problem opening the file." << std::endl;
  21. file.close();
  22. return -1;
  23. }
  24.  
  25. std::string line;
  26.  
  27. while(getline(file, line)) {
  28. if (std::all_of(begin(line), end(line), [](char c) { return std::isspace(c); })) continue;
  29.  
  30. // Set the answers
  31. if (line_number == 0) {
  32. answers = line;
  33. }
  34.  
  35. int student_grade {0};
  36.  
  37. // If line is students answers
  38. if (line_number != 0 && line_number % 2 == 0) {
  39. for (int answer = 0; answer < answers.length(); answer++) {
  40. if (line[answer] == answers[answer]) {
  41. student_grade += 1;
  42. }
  43. }
  44.  
  45. class_sum += student_grade;
  46.  
  47. std::cout << student_grade << std::endl;
  48.  
  49. } else if (line_number > 0) {
  50. // Line is students name
  51. std::cout << line << std::string(23 - line.length(), ' ');
  52. num_students += 1;
  53. }
  54.  
  55. line_number += 1;
  56. student_grade = 0;
  57. }
  58.  
  59. file.close();
  60.  
  61. std::cout << "\n--------------------------" << std::endl;
  62.  
  63. std::cout << "Average score " << " " << (class_sum / num_students) << std::endl;
  64.  
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement