fork(1) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. struct component
  5. {
  6. double quantity;
  7. std::string unit;
  8. std::string ingredient;
  9. };
  10.  
  11. std::istream& operator>>(std::istream& is, component & c)
  12. {
  13. component temp;
  14. is >> temp.quantity >> temp.unit;
  15. getline(is, temp.ingredient);
  16. if (is)
  17. c = temp;
  18. return is;
  19. }
  20.  
  21. void verbose(std::ostream& os, component const& c)
  22. {
  23. os << '\n';
  24. os << "quantity: " << c.quantity << '\n';
  25. os << "unit: " << c.unit << '\n';
  26. os << "ingredient: " << c.ingredient << '\n';
  27. }
  28.  
  29. int main() {
  30. for (component c; std::cin >> c; )
  31. verbose(std::cout, c);
  32. }
Success #stdin #stdout 0s 3480KB
stdin
1 cup sour cream
1 cup oil
1 teaspoon lemon juice
stdout
quantity:   1
unit:       cup
ingredient:  sour cream

quantity:   1
unit:       cup
ingredient:  oil

quantity:   1
unit:       teaspoon
ingredient:  lemon juice