@@ -33,6 +33,10 @@ std::multimap<std::string, std::string> smashing::load_dictionary(const std::str
33
33
// dictionary.insert({ key, value }); or
34
34
dictionary.emplace (key, value);
35
35
}
36
+ else
37
+ {
38
+ std::cout << " ***Invalid line\n " << line << " \n in " << filename << " ***\n\n " ;
39
+ }
36
40
}
37
41
}
38
42
else
@@ -55,13 +59,17 @@ std::pair<std::string, int> smashing::find_overlapping_word(std::string word,
55
59
if (lb != dictionary.end () &&
56
60
stem == lb->first .substr (0 , stem.size ()))
57
61
{
58
- return std::make_pair ( lb->first , offset) ;
62
+ return { lb->first , offset } ;
59
63
}
60
64
++offset;
61
65
}
62
- return std::make_pair ( " " , -1 ) ;
66
+ return { " " , -1 } ;
63
67
}
64
68
69
+ // Could use
70
+ // #include <format>
71
+ // #include <string_view>
72
+
65
73
// Listing 7.6 Simple answer smash game
66
74
void smashing::simple_answer_smash (
67
75
const std::map<std::string, std::string>& keywords,
@@ -70,10 +78,17 @@ void smashing::simple_answer_smash(
70
78
for (const auto & [word, definition] : keywords)
71
79
{
72
80
auto [second_word, offset] = find_overlapping_word (word, dictionary);
81
+ if (offset == -1 )
82
+ {
83
+ std::cout << " Not match for " << word << ' \n ' ;
84
+ continue ;
85
+ }
73
86
std::string second_definition = dictionary.at (second_word);
74
87
std::cout << definition << " \n AND\n " << second_definition << ' \n ' ;
75
88
76
89
std::string answer = word.substr (0 , offset) + second_word;
90
+ // Or more efficiently,
91
+ // std::string answer = std::format("{}{}", std::string_view(word).substr(0, offset), second_word);
77
92
78
93
std::string response;
79
94
std::getline (std::cin, response);
0 commit comments