fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. struct Student_info {
  7. friend std::ostream &operator<<( std::ostream &output,
  8. const Student_info &S ) { // overloads operator to print name and grades
  9. output << S.name << ": ";
  10. for (auto it = S.homework.begin(); it != S.homework.end(); ++it)
  11. std::cout << *it << ' ';
  12. return output;
  13. }
  14.  
  15. friend std::istream &operator>>( std::istream &input, Student_info &S ) { // overloads operator to read into Student_info object
  16. input >> S.name >> S.midterm >> S.final;
  17. double x;
  18. if (input) {
  19. S.homework.clear();
  20. while (input >> x) {
  21. S.homework.push_back(x);
  22. }
  23. input.clear();
  24. }
  25. return input;
  26. }
  27.  
  28. std::string name; // student name
  29. double midterm, final; // student exam scores
  30. std::vector<double> homework; // student homework total score (mean or median)
  31.  
  32. };
  33.  
  34.  
  35. int main() {
  36. //std::ifstream myfile ("/Users/.../Documents/C++/example_short.txt");
  37. Student_info student; // temp object for receiving data from istream
  38. std::vector<Student_info> student_list; // list of students and their test grades
  39. while (std::cin >> student) { // or myfile >> student
  40. student_list.push_back(student);
  41. student.homework.clear();
  42. }
  43. for (auto it = student_list.begin(); it != student_list.end(); ++it) {
  44. std::cout << *it << '\n';
  45. }
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0s 16064KB
stdin
Eunice 29 87 42 33 18 13 
Mary 71 24 3 96 70 14 
Carl 61 12 10 44 82 36 
Debbie 25 42 53 63 34 95 
stdout
Eunice: 42 33 18 13 
Mary: 3 96 70 14 
Carl: 10 44 82 36 
Debbie: 53 63 34 95