title: C++ binary search function inconsistent
body: <p>For the most part, the below binary search function I have created works. However, sometimes for no apparent reason, the search will not find an entry when I know it exists. Also sometimes, the variable of <code>search</code> will have what the user inputted, but with the first letter missing. I have appended a <code>cin.clear()</code> along with the <code>cin.ignore()</code> to try to fix the this weird error. So far, after adding the <code>cin.clear()</code> I have not been able to recreate the issue. </p>
  
  <p>View the program below:</p>
  
  <pre><code>void searchInventory(vector&lt;Vehicle&gt; &amp;carList)
  {
      cout &lt;&lt; "You have chosen to search for a vehicle.\n\n";
  
          int  first = 0,                     // first, last, and  middle define the vector element subscripts
               last = (carList.size() - 1),
               middle,
               position = -1,                 // Needed to keep track of current position
               searchResult = 0;              // Used copy the data from position for use in displaying confirmation messages to the screen
          bool found = false;                 // Flag to tell program if search has been successful
          string search;                      // Used to hold user's input on what to search
  
          do
          {
              // Prompt to user so they can enter the make they are searching for
              cout &lt;&lt; "Please enter the make of the vehicle you are searching for: ";
              cin.ignore();
              cin.clear();
              getline(cin, search);
  
              cout &lt;&lt; endl; 
  
              // Binary search algorithm begins...
              while (!found &amp;&amp; first &lt;= last)
              {
                  middle = (first + last) / 2;
                  if (carList[middle].getMake() == search)
                  {
                      found = true;
                      position = middle;
                  }
  
                  else if (carList[middle].getMake() &gt; search)
                      last = middle - 1;
  
                  else
                      first = middle + 1;
              }
  
              searchResult = position;  // position is copied into searchResult
  
              if (searchResult == -1)   // if searchResult is -1, then the user specified word was not found
              {
                  cout &lt;&lt; "Sorry, there is no vehicles under the make of " &lt;&lt; search &lt;&lt; " in your inventory\n"
                       &lt;&lt; "Please try again...\n\n";
              }
  
              else
              {
                  // If the make was found, the information about the first vehicle with the specified model is given
                  cout &lt;&lt; search &lt;&lt; "has been found...\n";
  
              }
  
          } while (searchResult == -1);
      }
  }
  </code></pre>
  
  <p>Some background on this function: I am searching a vector objects for a member variable of a string data type that contains the make of a car. </p>
  
  <p>So for example, if I search for "Ford" when "Ford" exists in the vector, the program most of the will say </p>
  
  <blockquote>
    <p>Ford has been found</p>
  </blockquote>
  
  <p>But other times the program will say </p>
  
  <blockquote>
    <p>Sorry, there is no vehicles under the make of Ford in your inventory</p>
  </blockquote>
  
  <p>And other times the program will say </p>
  
  <blockquote>
    <p>Sorry, there is no vehicles under the make of ord in your inventory</p>
  </blockquote>
  
  <p>Notice the "ord" instead of "Ford" in the last one.</p>
  
  <p>Does anyone have any clue why this binary search function can be so unpredictable? </p>