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. bool read(std::istream & is, Video& dvd);
  19. void print();
  20. };
  21.  
  22. std::istream& operator >> (std::istream& is, Video& vid)
  23. {
  24. std::string line;
  25. std::string theTitle, theGenre, theAvail, theHolds;
  26. if (std::getline(is, line))
  27. {
  28. std::istringstream iss(line);
  29. std::getline(iss, theTitle, ',');
  30. std::getline(iss, theGenre, ',');
  31. std::getline(iss, theAvail, ',');
  32. std::getline(iss, theHolds, ',');
  33. vid = Video(theTitle, theGenre, std::stoi(theAvail), std::stoi(theHolds));
  34. }
  35. return is;
  36. }
  37.  
  38. bool Video::read(std::istream & is, Video& dvd)
  39. {
  40. if (is.good())
  41. {
  42. is >> dvd;
  43. return true;
  44. }
  45. return false;
  46. }
  47.  
  48. void Video::print() {
  49. std::cout << "Video title: " << title << "\n" <<
  50. "Genre: " << genre << "\n" <<
  51. "Available: " << available << "\n" <<
  52. "Holds: " << holds << "\n";
  53. }
  54.  
  55.  
  56. int main()
  57. {
  58. Video dvd[10];
  59. int i = 0;
  60. while (i < 10 && dvd[i].read(std::cin, dvd[i]))
  61. {
  62. dvd[i].print();
  63. ++i;
  64. }
  65. }
  66.  
  67.  
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