binary search

Hey guys im trying to get a binary search 2 work but i cant seem 2 get it properly this is part of my code so far...

printMenu();

int num;
cin >> num;
cout << endl;


switch (num)
{
case 1:
cout << "Enter word" << endl;
cin >> word;
cout << endl;
toLowerCase(string word);
<--------This is were i need the function int binarySearch
break;
case 2:
dicPatt();
break;
case 3:
return EXIT_SUCCESS;
break;
default:
cout << "Invalid Choice" << endl;
}



system("pause");
return EXIT_SUCCESS;
}

void printMenu()
{
cout << "\tCrossword Helper " << endl << endl;
cout << " 1 : Check if word is in dictionary " << endl;
cout << " 2 : Find words in dictionary which match pattern " << endl;
cout << " 3 : Quit " << endl;
}



void toLowerCase(string word)
{
cout << "Enter word" << endl;
cin >> word;
cout << endl;
const int length = word.length();
for(int i=0; i < length; ++i)
{
word[i] = tolower(word[i]);
}
}

//This is the function i typed up but it doesnt work when i search for a name that is in my file it still returns -1....

int binarySearch(string array[], int size, string word)
{
int first = 0;
int last = size - 1;
int mid;

bool found = false;

while (first <= last && !found)
{
mid = (first + last) / 2;

if (array[mid] == word)
found = true;
else if (array[mid] > word)
last = mid -1;
else
first = mid + 1;
}
if (found)
return mid; <----i want to return the word if found e.g cout << word << "is in dictionary";
else
return -1; <------if does not find the word i want to have cout << word << "is not in dictionary";


}
Binary search? You're in luck, another user had that exact same problem! Here's the link:
http://www.cplusplus.com/forum/general/817/

Hey, wait a minute... That post was also made by you! Why are you asking the same question again? If we didn't know the solution the first time, we won't the second time.

"Only a fool continues to do the same thing over and over and expects a different outcome."
-Famous proverb
Topic archived. No new replies allowed.