データ構造の本で二分探索の疑似コードを読み、コードを書き始めました。私が書いたコードは次のとおりです。
#include <iostream.h>
#include <conio.h>
template <class T>
int BSearch(T x[], const int n, T item)
    {
    int loc, first = 0, found = 0, last = n-1;
        while(first <= last && !found)
        {
            loc = (first + last)/2;
            if(item < x[loc])
                last = loc - 1;
            else if(item > x[loc])
                first = loc + 1;
            else
                found = 1;
         }
      return found;
   }
int main()
    {
    const int n =5;
      int x[n],item;
      cout << "Pls enter " <<n<<" number(s): ";
      for(int i = 0; i < n;i++)
        cin >> x[i];
      cout << "Pls enter the item to Search: ";
        cin >> item;
      if(BSearch(x,n,item))
        cout << "\n\t Item Exist";
      else
        cout << "\n\t Item NOT Exist";
      getch();
      return 0;
   }
エラーはありませんが、論理障害があります。BSearch関数から0の値を返すだけで、「アイテムが存在しません」というメッセージが表示されます。私のバグはどこですか?見つかりませんでした。ありがとう