fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <vector>
  4. #include <cctype>
  5.  
  6. struct spell {
  7. std::string name;
  8. std::string effect;
  9. spell(std::string str) {
  10. std::istringstream strm(str);
  11. strm >> name >> effect;
  12. }
  13. };
  14.  
  15. struct spellbook {
  16. std::string title;
  17. std::string author;
  18. int pages;
  19. std::vector<spell> spells; // vector for nicer code and a cleaner name
  20. spellbook(std::string str) {
  21. std::istringstream strm(str);
  22. strm >> title >> author >> pages;
  23. }
  24. };
  25.  
  26. bool is_book(std::string str) {
  27. return isdigit(str.back());
  28. }
  29.  
  30. int main() {
  31. std::vector<spellbook> books;
  32. // ifstream fhand("Some/File/Location.txt");
  33. std::string line;
  34. while(std::getline(std::cin, line)) {
  35. if(is_book(line)) {
  36. books.push_back(spellbook(line));
  37. } else {
  38. books.back().spells.push_back(spell(line));
  39. }
  40. }
  41.  
  42. for(auto book : books) {
  43. std::cout << book.title << ", by: " << book.author << std::endl;
  44. std::cout << "Contains..." << std::endl;
  45. for(auto sp : book.spells) {
  46. std::cout << "\t" << sp.name << ": " << sp.effect << std::endl;
  47. }
  48. std::cout << std::endl;
  49. }
  50. }
Success #stdin #stdout 0s 4928KB
stdin
Dark_Secrets Grady 357
Evil_Force Dark
Grimoire Thompson 1967
Heavenly_Glow Light
Water_Ray Water
stdout
Dark_Secrets, by: Grady
Contains...
	Evil_Force: Dark

Grimoire, by: Thompson
Contains...
	Heavenly_Glow: Light
	Water_Ray: Water