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;
}

Two questions.
Is the file data in ascending order?

What is that for loop inside the first if of your binary search supposed to be doing? If you found that array[middle] is the value you seek, return middle, right there and then. That for loop just spins around a lot, and since s1 and s2 were never initialized, they probably will match as empty strings.

Comments
vmanes comments forced me to take a closer look at my program where I discovered the problem tthat I neeed to fix

jencas - good catch. I was just looking at the sort function.

meistrizy - you need a function that reads the data from the file into your names[] array. It ought to return the number of names actually read in, which should be used when passing the array to the sort, rather than using the size of the array. There might not be 20 names in the file.

Well...I modified my program but it still isn't reading the names in properly. It will decide whether or not it is true or false but not based on the input from the file. Since it is not reading the names in right it only spits out the last position. ???

#include <iostream>
#include <fstream>
using namespace std;

void selectSort(string[], int);
void binSearch (string[], int);

int main()
{
    const int size = 20;
    string array[size];
    int results;
    char ag;
    string peeps;
    
    binSearch(array, size);
        
    system ("pause");
    return 0;    
}

void selectSort(string array[], int s)
{
     int startScan, minIndex;
     string minValue;
     string peopleSearch;
     ifstream dataIn;
     dataIn.open("names.txt");
     
     if (!dataIn)
        cout << "There was an error opening up this file.";
     else 
     {
         for (int i; i < s; i++)
             getline(dataIn, array[i]);
            
         for (startScan = 0; startScan < (s - 1); startScan++)
         {
             minIndex = startScan;
             minValue = array[startScan];
             for (int index = startScan + 1; index < s; index++)
             {
                 if (array[index] < minValue)
                 {
                    minValue = array[index];
                    minIndex = index;
                 }
             }
             array[minIndex] = array[startScan];
             array[startScan] = minValue;
         }
     }
     dataIn.close();
}

void binSearch(string array[], int s)
{    
    string peeps;
    int    first = 0,
           last = s - 1,
           middle,
           position = 0;
    bool found = false;      
             
    cout << "Enter a name you would like to search for: \n";
    getline(cin, peeps);
    cout << endl;            
    
    selectSort(array, s);    
           
    while (first <= last && !found)
    {       
        middle = (first + last) / 2;                    
        if (array[middle] == peeps)
        {                              
            found = true;
            position = middle;            
        }
        else if (array[middle] > peeps)
        {
            last = middle - 1;
            position = last;
        }
        else 
        {
            first = middle + 1;
            position = first;
        }        
    }    
    
    if (found)
    {
         cout << endl;
         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
    {
         cout << endl;
         cout << "Congratulations! You have found " << peeps << " in
         the array \n";
         cout << "in position " << position << " of the array.";           
         cout << endl;
         cout << endl;
    }   
}

read and heed your compiler warnings.
This line

else 
	{
		for (int i; i < s; i++)         // THIS ONE
			getline(dataIn, array[i]);

uses an uninitialized value for i - so where's your data going?

Then, I would suggest you really look at your structure - it doesn't make a lot of sense.
Each function should do one thing, and one thing only. Your binsearch( ) gets the search target, then calls selectsort(), which does the actual program input?! before sorting the data.

Break these out so they are independent of each other. Sort sorts, search searches, input should be a separate function. Let main ask the user what to look for after data has been read and sorted. And don't assume that just because your data array is size 20 that you will always get exactly 20 data values from the file.

Thanks for your input and suggestions vmanes. My code works- I found that the problem was in my if/else statement. Results should == -1 indtead of results = -1.

Thanks for your input and suggestions vmanes. My code works- I found that the problem was in my if/else statement. Results should == -1 indtead of results = -1.

Well, there is that problem as well. Glad you got it all going.

I did follow your advice and initialize that variable in my for loop, moved the if/else statement to main but left the functions as is. Thanks again. :-)

This question has already been answered. Start a new discussion instead.