fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4. #include <string>
  5.  
  6. std::map<unsigned, std::vector<std::string>> rooms=
  7. {
  8. { 1, { "a pebble", "a boulder", "a corpse" } },
  9. { 2, { "a pink elephant", "a ballerina" } },
  10. { 3, {} },
  11. { 4, { "the end of the world" } }
  12. };
  13.  
  14. int main()
  15. {
  16. const char* prompt = "Which room would you like to visit?\n> ";
  17.  
  18. unsigned room_no;
  19. while (std::cout << prompt && std::cin >> room_no)
  20. {
  21. auto it = rooms.find(room_no);
  22.  
  23. if ( it == rooms.end() )
  24. {
  25. std::cout << "Unable to find the room number \"" << room_no << "\"\n";
  26. std::cout << "Terminating program.\n";
  27. return 0;
  28. }
  29.  
  30. if (!it->second.empty())
  31. {
  32. std::cout << "Room " << room_no << " contains:\n";
  33.  
  34. for (auto& item : it->second)
  35. std::cout << '\t' << item << '\n';
  36. }
  37. else
  38. std::cout << "Room " << room_no << " appears to be empty.\n";
  39. }
  40. }
Success #stdin #stdout 0s 3480KB
stdin
1 2 3 4 5
stdout
Which room would you like to visit?
> Room 1 contains:
	a pebble
	a boulder
	a corpse
Which room would you like to visit?
> Room 2 contains:
	a pink elephant
	a ballerina
Which room would you like to visit?
> Room 3 appears to be empty.
Which room would you like to visit?
> Room 4 contains:
	the end of the world
Which room would you like to visit?
> Unable to find the room number "5"
Terminating program.