0

I'm developing a c++ dictionary and I made a map of functions to search for words.

The program prompts the user to choose "search for a word" or "add a new word".

The problem: I made a map of functions that will be called if the user chooses to search for a word, but the program doesn't seem to be calling the function.

I'm new to map and haven't used it before today, some help would be great.

#include <iostream>
#include <map>
using namespace std; 
//map of function to search for word
map <string, int> search_word(map<string, int> &word_bank, int val)
{
    string word;
    cout<<"Search word: "<<endl;
    cin>>word;

    if(word_bank.find(word) != word_bank.end())
    cout<<word<<endl;
    else if (word_bank.find(word) == word_bank.end())
    cout<<"word not found";
}

int main()
{
    int choice;
    map <string, int> word_bank;
    word_bank["diingati"] = 0;
    word_bank["diperbuat"] = 1;
    word_bank["keibuan"] = 2;
    word_bank["kepercayaan"] = 3;
    word_bank["menggunakan"] = 4;
    word_bank["mempergunakan"] = 5;


    cout<<"***"<<" Welcome to C++ Malay Dictionary "<<"***"<<endl<<endl;
    cout<<"Do you want to search for a word or enter a new word? "<<endl<<endl;
    cout<<"Search : 1"<<endl<<"Enter new word: 2"<<endl<<endl;
    cin>>choice;

    if (choice == 1)
    map <string, int> search_word; //calling the function


    else
    cout<<"Please enter a number: ";
    cin>>choice;

}
Lucynda
  • 37
  • 1
  • 2
  • 9
  • You seems don't know how to call a function. You need [good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – llllllllll Apr 01 '18 at 14:53
  • Looks like you're new to C++ as well, since you might have to brush up how to call/invoke a function with parameters. You have to call your function like this `search_word( word_bank, 42 );` – P0W Apr 01 '18 at 14:53
  • You are not calling the function, but declaring another map with the same name as the function. Look how similar that line is to the one containing `word_bank`. – Bo Persson Apr 01 '18 at 14:53
  • @P0W what does `42` mean? is it the value of the key? – Lucynda Apr 01 '18 at 15:03

1 Answers1

0
 map <string, int> search_word; //calling the function

The comment is wrong. This doesn't call the function; it declares a variable called search_word of type std::map<std::string, int>.


Asides:

  • Turn your warnings up to max - you should get a warning that search_word doesn't actually return anything. You would also get a warning that the variable you declared called search_word was unused.

  • Think about what you want search_word to actually return - a map doesn't seem very useful. A bool or possibly even void would be better.

  • Search for, and read, Eric Lippert's posting called "How to debug small programs".