fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4.  
  5. struct Settler {
  6. int type; // Maybe some of those should be unsigned, but I just left them as you already had them
  7. int player;
  8. int x;
  9. int y;
  10. int health;
  11. };
  12.  
  13. std::istream& operator>>(std::istream& in, Settler& settler) {
  14. // Just read the values in the corresponding fields of Settler
  15. return in >> settler.type >> settler.player >> settler.x >> settler.y >> settler.health;
  16. }
  17.  
  18. std::ostream& operator<<(std::ostream& out, Settler const& settler) {
  19. out << "Type: " << settler.type << "\n";
  20. out << "Player: " << settler.player << "\n";
  21. out << "X:" << settler.x << "\n";
  22. out << "Y:" << settler.y << "\n";
  23. return out << "Health:" << settler.health << "\n";
  24. }
  25.  
  26. int main() {
  27. Settler current;
  28. std::vector<Settler> settlers;
  29. while(std::cin >> current) { // Read as long as it's possible to read a Settler
  30. settlers.push_back(current);
  31. }
  32.  
  33. // For demo, print out all settlers
  34. for(std::size_t i = 0; i < settlers.size(); ++i) { // In C++11, we could use for each syntax here
  35. std::cout << settlers[i];
  36. }
  37. }
Success #stdin #stdout 0s 2992KB
stdin
1 1 700 200 10
stdout
Type: 1
Player: 1
X:700
Y:200
Health:10