Get Wrong return value

Here is my code to find the index of a string array whose string is equal to the query string. I have checked the program can return the correct index, but the cout result is totally wrong. What's the problem? Thank you.

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
#include <iostream>
#include <string>

using namespace std;

int find(string a[], int low, int high, string target){
    int mid = low + (high-low)/2;
    if (low > high)
    	return -1;
    if (a[mid] == target)
    	return mid;
    
    if (a[mid] == ""){
    	int tmp1 = mid -1 ;
    	int tmp2 = mid +1 ;
    	while (a[tmp1] == "" &&  tmp1>0)
    	    tmp1--;
  	    while (a[tmp2] == "" && tmp2 < 12)
  	        tmp2++;
  	    if (target <= a[tmp1]){
  		find(a, low, tmp1, target);
  	    }else{
  		find(a, tmp2, high, target);
  	    }
  	}
 }

void main(){
	string s[13] = {"at", "", "", "", "ball", "", "", "car", "", "", "dad", "", ""};
	cout<< find(s, 0, 12, "car");
}

Output:
3601876請按任意鍵繼續 . . .
You promise the compiler that find will return a value in all cases. It does not.
I don't understand. Could you explain more? Thank you.
I got it. Thank you very much.
Topic archived. No new replies allowed.