fork(9) download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <vector>
  4. #include <string>
  5. #include <iterator>
  6. #include <cassert>
  7.  
  8. struct student
  9. {
  10. std::string name;
  11. std::string phone;
  12. std::string gender;
  13. std::string student_id;
  14. std::string email;
  15. };
  16.  
  17. int main()
  18. {
  19. std::vector<student> students;
  20.  
  21. std::string line;
  22. while(std::getline(std::cin, line))
  23. {
  24. std::istringstream ss(line);
  25. std::istream_iterator<std::string> begin(ss), end;
  26. std::vector<std::string> words(begin, end);
  27.  
  28. assert(words.size() >= 5);
  29.  
  30. int n = words.size() - 1;
  31. student s { words[0], words[n-3], words[n-2], words[n-1], words[n] };
  32. for (int i = 1 ; i < n - 3 ; i++) s.name += " " + words[i];
  33.  
  34. students.push_back(s);
  35. }
  36.  
  37. //printing
  38. for(auto && s : students)
  39. std::cout << "name = " << s.name << "\n"
  40. << "phone = " << s.phone << "\n"
  41. << "gender = " << s.gender << "\n"
  42. << "student_id = " << s.student_id << "\n"
  43. << "email = " << s.email << "\n\n";
  44. }
Success #stdin #stdout 0s 3044KB
stdin
Roger Pont 70778745 M 20120345 hills@school.edu
Tommy Holness 5127438973 M 20120212 tommy@school.edu
Lee Kin Fong 864564456434 F 30245678 fong@school.edu
stdout
name       = Roger Pont
phone      = 70778745
gender     = M
student_id = 20120345
email      = hills@school.edu

name       = Tommy Holness
phone      = 5127438973
gender     = M
student_id = 20120212
email      = tommy@school.edu

name       = Lee Kin Fong
phone      = 864564456434
gender     = F
student_id = 30245678
email      = fong@school.edu