/* Filename: diction.cpp Author: Br. David Carlson Date: July 14, 1999 Revised: December 23, 2001; April 26, 2022 This interactive program creates a map (table), where each entry is a pair containing a word and its definition. Thus the map is a small dictionary. The program then allows the user to repeatedly look up the meaning of words. In the code, change CTRL z to CTRL d for Linux. */ #include #include #include using namespace std; // Function prototypes: void LoadData(map & Dictionary); void Lookup(string Target, map & Dictionary); int main(void) { map Dictionary; string Target; LoadData(Dictionary); // Change to CTRL d for Linux: cout << "Enter word to look for (or CTRL z to quit): "; cin >> Target; while (!cin.fail()) { Lookup(Target, Dictionary); // Change to CTRL d for Linux: cout << "Enter word to look for (or CTRL z to quit): "; cin >> Target; } return 0; } /* Given: Target The word to look up. Dictionary The map containing the dictionary data. Task: To look up Target in the Dictionary map and print the meaning of Target if it was found, or a "not found" message otherwise. Return: Nothing. */ void Lookup(string Target, map & Dictionary) { map::iterator p; string Meaning; p = Dictionary.find(Target); if (p != Dictionary.end()) { cout << "Meaning is: " << p->second << endl; } else cout << "Word not found" << endl; } /* Given: Dictionary A map. Task: To add suitable data to Dictionary. Return: Dictionary The updated map. */ void LoadData(map & Dictionary) { // Note the 2 different ways of creating a pair to insert: Dictionary.insert(pair(string("hot"), string("having a high temperature"))); Dictionary.insert(make_pair(string("cold"), string("having a low temperature"))); Dictionary.insert(make_pair(string("jog"), string("run slowly"))); Dictionary.insert(make_pair(string("intelligent"), string("smart"))); Dictionary.insert(make_pair(string("register"), string("a high-speed storage location on a computer's CPU chip"))); }