Sorry for asking so much, but im doing lots of excersises and im having trouble with some things.

Now im doing a script with a Binary search function. But im having trouble with 2 things:

-I dont know how to know if the searchKey entered isnt in the given array.

-Im having trouble with the exiting, since sk is and int and "x" should be a char or a string. So if i initialize sk as a string, how can i convert it to an int?

Here's the script for you to understan what im talking about:

Code:

#include <iostream>
#include <cmath>
using namespace std;
int BinSearch(int data[], int numElements, int searchKey);
int main()
{
int data[]= {1, 4, 5, 6, 9, 14, 21, 23, 28, 31, 35, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79, 89, 95};
cout << "{";
for(int i=0; i<23; i++)
{
if(i != 0)
{
cout << ", ";
}
cout << data[i];
}
cout << "}" << endl;
int sk = 0;
while(sk != 'x' && sk != 'X')
{
cout << "Enter the search key (or \"x\" to exit): ";
cin >> sk;
cout << sk << " is in position " << BinSearch(data,23,sk) << endl;
}
cout << "Exiting..." << endl;
system("PAUSE");
}
int BinSearch(int data[], int numElements, int searchKey)
{
int mid = ceilf((float)numElements/2);
int result = 0;
while(true)
{
if(searchKey == data[mid])
{
result = mid;
break;
}
else if(searchKey > data[mid])
{
if(mid % 2 == 0)
{
mid += mid/2;
}
else
{
mid += ceilf((float)mid/2);
}
}
else if(searchKey < data[mid])
{
if(mid % 2 == 0)
{
mid -= mid/2;
}
else
{
mid -= ceilf((float)mid/2);
}
}
}
return result;
}