void wordChain(string file, string startWord) { map first2Letters; vector dictionary; string word; ifstream inputFile(file); if (inputFile) { while (inputFile >> word) { string firstTwo = word.substr(0, 2); first2Letters.insert(pair {firstTwo, word}); dictionary.push_back(word); } } inputFile.close(); cout << startWord << endl; for (auto w : dictionary) { string lastTwo = startWord.substr(startWord.length() - 2); map::iterator iter; iter = first2Letters.find(lastTwo); if (iter != first2Letters.end()) { string toRemove = startWord; dictionary.erase(remove(dictionary.begin(), dictionary.end(), toRemove), dictionary.end()); startWord = first2Letters[lastTwo]; cout << startWord << endl; } } }