Hello! So below, I have a piece of code that is part of a hangman game. What I am doing is asking for a guess, storing each guess in an array, and then trying to check and see whether the user has already guessed the letter or not. I'm not sure if I'm just not implementing the binary search algorithm right or if it has to do with the way I'm handling the bool aspect of the binarySearch function. There's something wrong in that regard, can you help me? I'm generally a little confused.
#include<iostream>
usingnamespace std;
bool binarySearch(char usedLetters[], int numGuessedLetters, char guess);
void bubbleSort(char usedLetters[27], int used);
int main()
{
char guess;
char usedLetters[26];
int numGuessedLetters = 0;
int i = 0;
do{
cout << "Please guess a letter." << endl;
cin >> guess;
//Putting each guess into an array and remembering how many letters have been guessed
usedLetters[numGuessedLetters++] = guess;
//Using bubblesort function to alphebatize the list of the users guessed letters
bubbleSort(usedLetters, numGuessedLetters);
//Searches to see if user has already guessed that letter or not
binarySearch(usedLetters, numGuessedLetters, guess);
if (true)
cout << "Been guessed!" << endl;
else
cout << "Letter has not been guessed!" << endl;
i++;
} while (i < 5);
cout << "\n\n";
system("pause");
return 0;
}
bool binarySearch(char usedLetters[], int numGuessedLetters, char guess)
{
int low = 0;
int high = numGuessedLetters - 1;
int mid;
while (low <= high)
{
mid = (low + high) / 2;
if (guess == usedLetters[mid])
{
returntrue;
}
elseif (guess > usedLetters[mid])
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
void bubbleSort(char usedLetters[27], int numGuessedLetters)
{
for (int x = 0; x < numGuessedLetters; x++)
for (int x = 0; x < numGuessedLetters; x++)
{
for (int i = 0; i<numGuessedLetters - 1; i++)
{
if (usedLetters[i]>usedLetters[i + 1])
{
int temp = usedLetters[i + 1];
usedLetters[i + 1] = usedLetters[i];
usedLetters[i] = temp;
}
}
}
}