title: Binary Search For an Array in C++
body: <p>My Binary search keeps returning zero I'm not sure why. Maybe I'm passing my arguments in incorrectly? Or maybe I'm not returning it right? I'm not quite sure what is wrong, if anyone could explain how I can return the values I want instead of zero thanks. my array  is filled with words I didn't include that part of my code because it is being filled in by a file. I'm not entering the first word so I shouldnt be getting zero. I've tried multiple words.</p>
  
  <pre><code>     using namespace std;
       #include &lt;iostream&gt;
       #include &lt;fstream&gt;
       #include &lt;string&gt;
  
  
       //function prototypes
       void selectionSort(string[], int);
       int binarySearch(string[], int, string);
  
       int main()
    {
  int open;
  int name[8];
  ifstream inputFile;
  string filename;
  int i = 0;
  int size = 1024;
  int newSize;
  string exit;
  string words[1024];
  string word;
  string newArray[1024];
  string search;
  int results;
  string createdFile;
  int startScan, minIndex, minValue;
  //newArray[0]="This";
  
  
  cout &lt;&lt; "Please Enter the Filename: ";
  cin &gt;&gt; filename;
  inputFile.open(filename.c_str());
  if (!inputFile) {
      cout &lt;&lt; "Your File could not be opened!";
  }
  else if (inputFile) {
  
      while (i&lt;1024) {
          inputFile &gt;&gt; word;
          //cout &lt;&lt; i;
          if (word.length()&lt;2) {              //gets rid of single character words
          i++;
          }
          else if(inputFile.eof()) {
          //cout&lt;&lt;newSize&lt;&lt;endl;
          newSize=i;
          //cout&lt;&lt; "DONE!"&lt;&lt;newSize;
          break;
          }
          else{
          newArray[i] = word;
          i++;
          //cout&lt;&lt;newArray[i]&lt;&lt;" " ;
          //newSize=1;
  
  
  
          //cout&lt;&lt;newSize;
          }
  
          //cout &lt;&lt; newArray;
          //i++;
      }
  
  
  
  
  }
  inputFile.close();
  //Take values stored in array and put them in another array with i number of elements
  
      string finalArray[i];
      size=i;
      for(int j = 0; j&lt;i;j++)
          {
          finalArray[j]=newArray[j];
  
          //cout&lt;&lt;finalArray[j];
          }
  
  cout &lt;&lt; "Your document has been sorted. " &lt;&lt; endl;
  selectionSort(finalArray, size);
  cout&lt;&lt; "Out putting Your array"&lt;&lt;endl;
  for (int z = 0; z&lt;size; z++) {
      cout &lt;&lt; finalArray[z] &lt;&lt; " ";
  }
  
      cout &lt;&lt; "Please insert a word to search!: " &lt;&lt; endl;
      cin &gt;&gt; search;
      results = binarySearch(finalArray, size, search);
      cout &lt;&lt; results &lt;&lt; endl;
      //results = binarySearch(finalArray, size, search);
      //cout &lt;&lt; results;
      cout &lt;&lt; "To exit press 0, or enter another word..." &lt;&lt; endl;
  
  
  
  }//end of main
  
  
     void selectionSort(string array[], int size) {
  int startScan, minIndex;
  string minValue;
  
  for (startScan = 0; startScan &lt; (size - 1); startScan++)
  {
      minIndex = startScan;
      minValue = array[startScan];
      for (int index = startScan + 1; index &lt;size; index++) {
          if (array[index]&lt;minValue) {
              minValue = array[index];
              minIndex = index;
          }
      }
      array[minIndex] = array[startScan];
      array[startScan] = minValue;
  
  }
     }
  
  
     int binarySearch(string words[], int numElems, string word) {
  
  int first = 0,                 //first array element
      last = numElems - 1,            //last array element
      middle,                        //mid point of search
      position = -1;                 //position of search value
  bool found = false;            //flag
  
  while (!found &amp;&amp; first == last)
  {
      middle = (first + last) / 2; //this finds the mid point
      if (words[middle] == word) {
          found = true;
          position = middle;
      }
      else if (words[middle]&gt; word) // if it's in the lower half
      {
          last = middle - 1;
      }
      else {
          first = middle + 1;                 //if it's in the upper half
      }
      return position;
  
       }
     }//end binarySearch
  </code></pre>