fork download
  1. #include <string>
  2. #include <istream>
  3. #include <sstream>
  4. #include <iostream>
  5.  
  6. class Video
  7. {
  8. std::string title;
  9. std::string genre;
  10. int available;
  11. int holds;
  12.  
  13. public:
  14. Video(std::string title_, std::string genre_, int available_, int holds_) :
  15. title(title_), genre(genre_), available(available_), holds(holds_) {}
  16. Video() : available(-1), holds(-1) {}
  17. friend std::istream& operator >> (std::istream& is, Video& vid);
  18. void print();
  19. };
  20.  
  21. std::istream& operator >> (std::istream& is, Video& vid)
  22. {
  23. std::string line;
  24. std::string theTitle, theGenre, theAvail, theHolds;
  25. if (std::getline(is, line))
  26. {
  27. std::istringstream iss(line);
  28. std::getline(iss, theTitle, ',');
  29. std::getline(iss, theGenre, ',');
  30. std::getline(iss, theAvail, ',');
  31. std::getline(iss, theHolds, ',');
  32. vid = Video(theTitle, theGenre, std::stoi(theAvail), std::stoi(theHolds));
  33. }
  34. return is;
  35. }
  36.  
  37. void Video::print() {
  38. std::cout << "Video title: " << title << "\n" <<
  39. "Genre: " << genre << "\n" <<
  40. "Available: " << available << "\n" <<
  41. "Holds: " << holds << "\n";
  42. }
  43.  
  44.  
  45. int main()
  46. {
  47. Video dvd[10];
  48. int i = 0;
  49. while (i < 10 && std::cin >> dvd[i])
  50. {
  51. dvd[i].print();
  52. ++i;
  53. }
  54. }
  55.  
Success #stdin #stdout 0s 15248KB
stdin
Legend of the seeker, Fantasy/Adventure, 3, 2
Mindy Project, Comedy, 10, 3
Orange is the new black, Drama/Comedy, 10, 9
stdout
Video title: Legend of the seeker
Genre:  Fantasy/Adventure
Available: 3
Holds: 2
Video title: Mindy Project
Genre:  Comedy
Available: 10
Holds: 3
Video title: Orange is the new black
Genre:  Drama/Comedy
Available: 10
Holds: 9