I am having difficulties with the binarysearch function. I managed to get sorting and binary search done. However when I search a name it has to be the entire name for example Allison, Jeff. However, I want to know how to search just the first or last name of the person and have a result come back. So rather than entering the entire name Allison, Jeff I could just type Jeff and a result would come up. Thanks inadvance! here is my code:

#include <iostream> //for cin and cout
#include <string> //for string use
#include <iomanip> //for better format
#include <conio.h> //for getch

using namespace std;

    //Function Prototypes:
    void sortNames(string[], int); 
    void showSorted(string [], int); 
    int binarySearch(string [], int, string); 


int main()
{
    //List size, and the names in the list
    const int SIZE = 20;
    string name[SIZE] = {"Collins, Bill", "Smith, Bart", "Allen, Jim",
                         "Griffin, Jim", "Stamey, Marty", "Rose, Geri",
                         "Taylor, Terri", "Johnson, Jill",
                         "Allison, Jeff", "Looney, Joe", "Wolfe, Bill",
                         "James, Jean", "Weaver, Jim", "Pore, Bob",
                         "Rutherford, Greg", "Javens, Renee",
                         "Harrison, Rose", "Setzer, Cathy",
                         "Pike, Gordon", "Holland, Beth" };
    //Variables
    string NameSearch; //user input for name
    int searchResult; //holds the searched results

    //Main Title
    cout << "\t\t\tEmployee Name Directory\t\t\t" << endl
         << "--------------------------------------------------------------------------------" << endl;

    //Names are sorted and then displayed
    //Functions are called
    cout << "Here is the list in alphabetical order by last name: " << endl; 
    sortNames(name, SIZE);
    showSorted(name, SIZE); 

    //Get user input for name
    cout << "Enter the name you wish to find (ex: Lastname, Firstname): ";
    getline(cin,NameSearch);
    cout << endl;

    //Function is called and results stored into variable searchResult
    //Display message for results found or not found.
    searchResult = binarySearch(name, SIZE, NameSearch);
    if (searchResult == -1) 
    {
        cout << "============================= No Results Found! ================================" << endl
             << "Sorry, the name you searched does not exist in the list." << endl;
    }
    else 
    {
        cout << "============================= Results Found! =================================" << endl
             << "One search result was found in the list for " << NameSearch << endl;
    }
    _getch(); //Hold screen
}
//------------------------------------------------------------------
// Definition of Function sortNames.
// Two paramters NameSorting and listSize.
// NameSorting holds the all names and listSize holds the list size.
// The function sorts the names by alphabetical order by last name.
//------------------------------------------------------------------
void sortNames(string NameSorting[], int listSize)
{
    string tempName;
    for(int namesearch = 0; namesearch < listSize - 1; namesearch++)
    {
        for(int found = 0; found < listSize - 1; found++)
        {
            if (NameSorting[found] > NameSorting[found+1]) 
            {
                tempName = NameSorting[found];
                NameSorting[found] = NameSorting[found + 1];
                NameSorting[found + 1] = tempName;
            }
        }
    }
}
//------------------------------------------------------------------
// Definition of Function showSorted.
// Two paramters name and listSize.
// Paramter name holds the all the names in the list. The parameter 
// listSize holds the list size which is 20.
// The function displays the sorted list using a for loop.
//------------------------------------------------------------------
void showSorted(string name[], int listSize) 
{ 
for (int count = 0; count < listSize; count++) 
cout << name[count] << " "<<endl; 
cout << endl; 
}
//------------------------------------------------------------------
// Definition of Function binarySearch.
// Three parameters nameSearchedfor, listSize, and Input.
// nameSearchedfor holds the searched result for the name entered.
// listSize holds the list size, and the parameter Input holds the 
// name entered by the user.
// The function searches the array list for the name entered by the
// user. Recursion is used.
//------------------------------------------------------------------
int binarySearch(string nameSearchedfor[], int listSize, string Input)
{
    int first = 0,
    last = listSize - 1,
    middle,
    position = -1;

    bool found = false;

    while(!found && first <=last)
    {
        middle = (first + last)/2;
        if (nameSearchedfor[middle] == Input)
        {
            found = true;
            position = middle;
        }
        else if (nameSearchedfor[middle] > Input)
        {
            last = middle - 1;
        }
        else 
        {
            first = middle + 1;
        }
    }
    return position;
}

I think you will want to use a structure or class to hold the names so that it's easier to search on just one of the fields. But ... if you want to use binary search then the entire array will have to be sorted by either the first or last names before beginning the search.

struct names
{
   string lastname;
   string firstname;
};

names NameArray[SIZE];

I think its only polite for me to add this to dragon's.
You should have functions to handle your code as it is very unconfortable to go through your code.
its really not confortable at all.

Why does this code look similar to me? Hmmmm. Are you and Beastie805 doing the same assignment or something? :-P

how can i use this code to search for the name of an object, document, or musicfile or something in my computer?

This article has been dead for over six months. Start a new discussion instead.