Thanks in advance for your help. The problem is this: I am supposed to read an array from a file and ask the user to input a name to search for within the file. If the name is there then return the position number in the file, if not output an error message. I get my program to read the file but when it searches for the name it always comes back false. What am I missing? Here is my code:
#include <iostream>
#include <fstream>
using namespace std;
int binarySearch(string[], int, string);
const int SIZE = 20;
int main()
{
//define array and input
string names[SIZE];
string peopleSearch;
int results;
char ag;
ifstream dataIn;
dataIn.open("sortedNames.txt");
do
{
//get user data
cout << "Enter the name of the person you would like to
search for (Last, First): \n";
cin.ignore();
getline(cin, peopleSearch);
cout << endl;
//search for the person
results = binarySearch(names, SIZE, peopleSearch);
//return -1 if person not found
if (results = -1)
{
cout << "That person does not exist in this array. You might
try \n";
cout << "www.randyspeoplesearch.com. That is a great site
for finding people.";
cout << endl;
cout << endl;
}
else
{
//Otherwise results contain info
cout << "Congratulations! You have found "
<< peopleSearch << " in the array \n";
cout << "in position " << results << "of the array.";
cout << endl;
cout << endl;
}
cout << "Would you like to run another person search? Y/N: ";
cin >> ag;
cout << endl;
}while (ag == 'y' || ag == 'Y');
dataIn.close();
system("pause");
return 0;
}
int binarySearch(string array[], int size, string value)
{
int first,
last = size - 1,
middle,
position = -1;
bool found = false;
string s1, s2;
int index;
int pos;
while (!found && first <= last)
{
middle = (first + last) / 2;
if (array[middle] == value)
{
for(int i = 0; i < size; i++)
{
for(int h = 0; h < size; h++)
{
if (s1 == s2)
{
found = true;
position = middle;
}
}
}
}
else if (array[middle] > value)
last = middle - 1;
else
first = middle + 1;
}
return position;
}