fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <map>
  5.  
  6. using namespace std;
  7.  
  8. string getFileData()
  9. {
  10. string data;
  11.  
  12. data += "6\n";
  13.  
  14. data += "b\n"; data += "Second Letter in the Alphabet (Rhymes with 'Bee')\n";
  15. data += "a\n"; data += "The first letter in the alphabet (Vowel)\n";
  16. data += "why\n"; data += "Why what?\n";
  17. data += "why?\n"; data += "Why what?\n";
  18. data += "hello\n"; data += "Hello!\n";
  19. data += "hi\n"; data += "Hello!\n";
  20.  
  21. return data;
  22. }
  23.  
  24. int main()
  25. {
  26. // let's pretend this is our file
  27. stringstream file(getFileData());
  28.  
  29. int n;
  30.  
  31. file >> n; // get number of entries
  32. file.get(); // ignore newline
  33.  
  34. map<string, string> dataMap;
  35.  
  36. for (int i = 0; i < n; ++ i)
  37. {
  38. string key, value;
  39.  
  40. getline(file, key);
  41. getline(file, value);
  42.  
  43. dataMap[key] = value;
  44. }
  45.  
  46. // no need to sort. map
  47. // sorts automatically by key
  48.  
  49. string input;
  50.  
  51. while (true)
  52. {
  53. getline(cin, input);
  54.  
  55. if (input.empty()) break; // empty line == quit
  56.  
  57. // make everything lowercase
  58. for (int i = 0; i < input.length(); ++ i)
  59. if (input[i] >= 'A' && input[i] <= 'Z')
  60. input[i] += 'a' - 'A';
  61.  
  62. // if the key exists, print the respective value
  63. if (dataMap.find(input) != dataMap.end())
  64. cout << dataMap[input] << endl;
  65. // otherwise, print an error message
  66. else cout << "Sorry, I don't understand..." << endl;
  67. }
  68. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty