fork(1) download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. struct Customer
  6. {
  7. std::string name;
  8. std::string id;
  9. float loanAmount;
  10. };
  11.  
  12. std::istream& operator >> (std::istream& is, Customer& cust)
  13. {
  14. std::getline(is, cust.name); // getline from <string>
  15. is >> cust.id;
  16. is >> cust.loanAmount;
  17. is.ignore(1024, '\n'); // after reading the loanAmount, skip the trailing '\n'
  18. return is;
  19. }
  20.  
  21. int main()
  22. {
  23. std::stringstream ss;
  24.  
  25. ss << "Williams, Bill\n"
  26. << "567382910\n"
  27. << 380.86f << "\n"
  28. << "Davidson, Chad\n"
  29. << "435435435\n"
  30. << 400.00f;
  31.  
  32. Customer c;
  33. ss >> c;
  34. std::cout << c.name << "\t" << c.id << "\t" << c.loanAmount << std::endl;
  35.  
  36. ss >> c;
  37. std::cout << c.name << "\t" << c.id << "\t" << c.loanAmount << std::endl;
  38. }
  39.  
Success #stdin #stdout 0.02s 2816KB
stdin
Standard input is empty
stdout
Williams, Bill	567382910	380.86
Davidson, Chad	435435435	400