为什么我的二进制搜索没有返回要显示的名称?

时间:2013-11-18 23:39:51

标签: c++

很抱歉,但我无法弄清楚为什么我的代码,更具体地说二进制searchName()函数没有返回要显示的名称。我已经验证了名称是使用显示功能排序的,这是分配不需要的。任何帮助表示赞赏。这是代码开始结束。我们还没有涵盖向量或除阵列以外的任何东西,因此向量无济于事,但我不能等到我们这样做。

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//function Prototypes
void nameSort(string friendArray[], int ARRAY_SIZE);
//function to display names from file
void displayNames(string friendArray[], int counter);
//function to binary search for name in file/array 
//How do i return a string?? I got the following code from web searches
int searchName(string friendArray[], int ARRAY_SIZE, string result);

int main()
{
    const int ARRAY_SIZE = 200; //declare array size
    string friendArray[ARRAY_SIZE];//array of 200 strings
    ifstream fileIn; //create file object

    int counter = 0;
    string choice;
    string result; //variable to hold the search result

    fileIn.open("myFriends.dat");//open the file
    if(fileIn.fail())//test to see if file opened
    {
        cout<<"Check to see if file is in same directory ";
        cout<<"as the .cpp file."<<endl;
    }

    while(getline(fileIn, friendArray[counter]))
    {
        counter++;
    }

    //call function to sort array
    nameSort(friendArray, ARRAY_SIZE);
    //display sorted names to screen with function
    displayNames(friendArray, ARRAY_SIZE);
    //call to search function
    searchName(friendArray, ARRAY_SIZE, result);
    cout<<friendArray[]<<" is my friend."<<endl;

    system("Pause");
    return 0;
}

//sort function
void  nameSort(string friendArray[], const int ARRAY_SIZE)
{
    bool swap;
    string temp;

    do
    {
        swap = false; //set flag to false
        for(int counter = 0; counter < (ARRAY_SIZE - 1); counter++)
        {
            if(friendArray[counter] > friendArray[counter + 1])
            {
                temp = friendArray[counter];
                friendArray[counter] = friendArray[counter + 1];
                friendArray[counter + 1] = temp;
                swap = true;
            }
        }//close for loop
    }while(swap);
}//end of function

//display names function

void displayNames(string friendArray[], const int ARRAY_SIZE)
{
    for(int index = 0; index < ARRAY_SIZE; index++)
        cout<<friendArray[index]<<endl;
}

//binary search function
int searchName(string friendArray[], const int ARRAY_SIZE, string result)
{
    int first = 0;
    int last = (ARRAY_SIZE - 1);
    int middle;
    int position = -1;
    string name;
    bool found = false;

    cout<<"Please enter a name or END to quit.";
    cin>>name;

    while(!found && first <= last && name != "END")
    {
        middle = (first + last) / 2;

        if(friendArray[middle] == result)
        {
            found = true;
            position = middle;
        }
        else if(friendArray[middle] > result)
            last = (middle - 1);
        else 
            first = (middle + 1);
    }
    return position;
}

1 个答案:

答案 0 :(得分:3)

Rup是对的。尝试将代码更改为

int friendIndex = searchName(friendArray, ARRAY_SIZE, result);
cout<<friendArray[friendIndex]<<" is my friend."<<endl;

我忘了你应该先检查friendIndex == -1。这会更好:

int friendIndex = searchName(friendArray, ARRAY_SIZE, result);
if(friendIndex >= 0)
    cout<<friendArray[friendIndex]<<" is my friend."<<endl;
else
    cout<<result<<" is not my friend."<<endl;