fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <stdexcept>
  5.  
  6. struct itemInfo
  7. {
  8. int quantity;
  9. std::string name;
  10. double price;
  11. };
  12.  
  13. int main()
  14. {
  15. std::string line;
  16. std::vector<itemInfo> items;
  17.  
  18. while (std::getline(std::cin, line))
  19. {
  20. try
  21. {
  22. itemInfo item;
  23.  
  24. auto start = line.find_first_not_of(" \t");
  25. auto stop = line.find_first_of(" \t", start + 1);
  26. item.quantity = std::stoi(line.substr(start, stop - start));
  27.  
  28. start = line.find_first_not_of(" \t", stop + 1);
  29. stop = line.find(" at ", start);
  30. item.name = line.substr(start, stop - start);
  31.  
  32. start = line.find_first_not_of(" \t", stop + 4);
  33. item.price = std::stod(line.substr(start));
  34.  
  35. items.push_back(item);
  36. }
  37. catch (const std::logic_error &) { }
  38. }
  39.  
  40. // use items as needed...
  41.  
  42. for(auto &item : items)
  43. {
  44. std::cout << item.name << ": " << item.quantity << " @ " << item.price << std::endl;
  45. }
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0.01s 5520KB
stdin
1 toy at 10.65
2  box of books at 12.11
1 pot plant at 6.45
stdout
toy: 1 @ 10.65
box of books: 2 @ 12.11
pot plant: 1 @ 6.45