fork download
  1. #include <string>
  2. #include <sstream>
  3. #include <iostream>
  4. class Person {
  5. std::string ID;
  6. std::string name;
  7. std::string address;
  8. std::string phone;
  9. public:
  10. Person(std::string ID0, std::string name0, std::string address0, std::string phone0)
  11. : ID(ID0), name(name0), address(address0), phone(phone0) {}
  12. Person() {}
  13.  
  14. void show() const {
  15. std::cout << "Person created with ID " << ID << " and phone " << phone << '\n';
  16. }
  17.  
  18. friend std::istream& operator>>(std::istream& is, Person& p)
  19. {
  20. getline(is, p.ID);
  21. getline(is, p.name);
  22. getline(is, p.address);
  23. getline(is, p.phone);
  24. return is;
  25. };
  26. };
  27.  
  28. int main()
  29. {
  30. std::istringstream test("529173860\n"
  31. "Dick B. Smith\n"
  32. "879 Maple Road, Centralia, Colorado 24222\n"
  33. "(312) 000-1000\n"
  34. "925173870\n"
  35. "Harry C. Anderson\n"
  36. "635 Main Drive, Midville, California 48444\n"
  37. "(660) 050-2200");
  38. Person p;
  39. while(test >> p)
  40. {
  41. p.show();
  42. }
  43. }
  44.  
Success #stdin #stdout 0.01s 2860KB
stdin
Standard input is empty
stdout
Person created with ID 529173860 and phone (312) 000-1000
Person created with ID 925173870 and phone (660) 050-2200