Seeing if letter was guessed already or not

Hello, with this binary search I'm trying to make sure that the user doesn't guess a word twice in my hangman game. It seems to be working correctly except for the fact that I have to enter a letter twice before it gets rejected instead of once? I think that this is a logic error but I can't figure it out... maybe having to do with where my bubblesort(sorts list alphabetically for binary search to work) is placed... ah please help!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
binarySearch(usedLetters, numGuessedLetters, guess);

		do{
			//Input 
			cout << "<<<<<<<<<<MAKE A GUESS>>>>>>>>" << endl; 
			cout << "\n\nPlease enter a letter to guess: ";
			cin >> guess;


			bool returned;
			returned = binarySearch(usedLetters, numGuessedLetters, guess);
			if (returned == false)
			{
				cout << "" << endl;
				x = 4; 
			}

			else if (returned == true)
			{
				cout << "You've already used this letter." << endl;
				x = 5; 
			}
		} while (x == 5);   

bool binarySearch(char usedLetters[], int numGuessedLetters, char guess)
{
	bool found = false;
	int first = 0;
	int last = numGuessedLetters + 1;
	int mid = 0;
	while (first <= last)
	{
		mid = (first + last) / 2;
		if (usedLetters[mid] == guess)
		{
			return true;
		}
		else if (usedLetters[mid] > guess)
		{
			last = mid - 1;
		}
		else
		{
			first = mid + 1;
		}
	}
	return (false);
}
Correct me if I wrong
I think it's because of int last = numGuessedLetters + 1; if numGuessdLetters is 0 then mid=(1+0)/2, that can be 0 or 1. If the numGuessedLetters is 0 that mean usedLetters[] is still empty right ? So if (usedLetters[mid] == guess) is invalid because guess is compared with nothing or anything since usedLetters[0] and useLetters[1] is undefined
Ahhh thankyou kind person! I re-initialized numGuessedLetters to 1. It's now working perfectly :D You rock!
Topic archived. No new replies allowed.