Binary search problem - always returns -1

Hello, I am working on a project for school. I need to search through an array of strings in order to be able to delete a particular entry in the array. However my binary search function keeps returning -1 (not found). I can't figure out why.

I can print the array, quick sort the array, add to it, but my search function is failing, which means that I can't delete an entry. I sort the array before I search it.

Could anyone please help my understand why it returns -1 no matter what?
Thank you for your 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
//the array
#define LIMIT 50;
typedef struct StudentRecord
{
     string name;
     string address;
}StudentRecord;
StudentRecord students[LIMIT]; // there are 35 entries in the array, but the        
                               //LIMIT is set to 50

//call to the function
int index;
string searchTerm;
cout << "Enter a name to search for: "; // I only need to search my the name
fflush(stdin);
getline(cin,searchTerm); 
index = biSearch(students, 0, LIMIT, searchTerm); // there are about 15 //empty entries in the array. I tried to only search those records that had data //in them, but it still failed.

// biSearch function definition

int biSearch(StudentRecord students[LIMIT], int start, int finish, string searchTerm) 
{
    int mid = (start+finish)/2;
    if (finish-start+1 > 0)
    {
		if (students[mid].name == searchTerm)
        return mid;
       else
		   if (searchTerm < students[mid].name)
		   return biSearch(students, start, mid-1, searchTerm);                  
       else
		   return biSearch(students, mid+1, finish, searchTerm);   
    }    
    else
      return -1;
}
My guess is that you don't want the undefined behavior caused by flushing an input stream on line 15, so I would suggest removing that line. You may want to substitute: cin >> ws; to consume any leading whitespace before using getline.

Otherwise the code looks reasonable, assuming case matches and the array has been correctly populated and sorted. Of course, those are big assumptions.
I removed the line 15 and used cin >> searchTerm, but I am still getting -1 as a response.
I am starting to think that the search is okay, but something else is wrong. I will double check the sort function, but it looks okay, especially since the list is sorted property when I print it out. I really don't understand what is going on. I will post an answer if I figure it out.
I found out why the search didn't work.

It didn't work because when I read the names from the file into the array, the program read the mane with a space after it, so even though the search found a match, it wasn't exact match, because it had a space in there.

The problem is that I thought about it and told it to dump the space at the end using the string.erase function. It just didn't do it.

Also, when I was debugging in Visual Studio, debugger was telling me the the value in the search term was without a space, which was incorrect.

So I got lost in this problem for a while, but I got it now.

Thanks.
Topic archived. No new replies allowed.