have a dictionary program that loads a txt file into a vector then searches based on user input and returns the word, type and definition.
The search doesn't have to be fancy so its just a comparison.
The problem I am having is when I put my function and the code into the switch statement it wont function and immediately returns 0 or whatever number I have determined to return. It does this before the function executes and that means I cant ever execute.
Is this something to do with the switch statement?
int searchForWord(vector <Word>& Dictonary, const string searchWord) {
for (Word word : Dictonary)
{
if (searchWord == word.DictonaryWord )
{
cout << "Word has been found " << endl;
// This line assigns the type to the value and finds in map and then points to second value of key pair
string value = DiffTypes.find(word.Type)->second;
cout << word.DictonaryWord << ' ';
cout << value << endl;
cout << word.Definition << endl;
return 1 ;
}
}
return 0;
}
do
{
cout <<
"\n Welcome to the Dictionary\n""----------------------------------------------------\n"" Please select an option from the below menu:\n""----------------------------------------------------\n""\n1. Enter a word to search\n""2. Find words with a specified number of characters \n""3. List all words that have a q without a following u\n""4. Quit\n" << endl;
cout << "Please select an option: " << endl;
cin >> menuChoice;
switch (menuChoice)
{
case 1:
cout << "Please enter a word to search: \n";
getline(cin, searchWord);
searchForWord(Dictonary, searchWord);
if (searchForWord(Dictonary,searchWord)==0) {
cout << " word not found" << endl;
break;
}
break;
If I move this block out of the switch it works as intended and can search as needed.