I am writing a program that reads words from a file and uses a binary search to determine how many compares the program does while searching for a word that the user inputs. I got everything worked out except an issue I am having with me if statment in my function. I think that is where my problem is. Can someone give me a clue? I am lost!

[code=c++]
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int binarySearch(string [], int, string); //functin prototype
const int size=60000; //size of my array
int iCompares; //setting my compare variable global


int main ()
{
    string totalwords[size]; // array that will hold words from file
    int result; // verifies if the entered word is in the file
    string word; // string
    int iCompares;

    fstream sample6; // open file

    sample6.open("sample6.txt"); //verify that file opens
    if (!sample6)
        //Error if file doesn't open
        cout<< "There was an error unable to read file!";
    cout<< endl;


    //prompt user to enter a word
    cout<< "Enter the word you wish to search for: ";
    //the user's word is stored in variable word
    cin >> word;


    int CurrentLine=0;
    getline(sample6, totalwords[CurrentLine++]);
// Reads in words from file
    while(!sample6.eof())
    if (result== -1)
          { //error is user's word is not in file
               cout<< "Sorry, that word is not in this file. ";
          }
    else
          {// Displays results once word was found

              cout<< word << " was found"<< endl;
              cout<< "The search conducted took ";
              cout << result << " compares";
           }
result= binarySearch( totalwords,size, word);


   return 0;
}

/* This function is to determine how many compares it took
for the word to be found.
*/
int binarySearch(string array [], int element, string word)
{int index;
    int first=0; // initializing variable
    int last = element-1; //have to subtract one b/c of null
    int middle; //declare middle variable
    int place= -1; // problem????
    bool found=false; //determine if the number is found by bool value
cout<< "owl";
    while (!found && first <=last)//while the word is not found do this loop
    {
        cout<< "cat";
        iCompares +=2;
        middle=(first+last)/2; // find middle value
        cout<<"dog";

//this statement is the problem..i think.
        if(array[middle] == word)
        {// check if middle number is what user inputted
            found=true;
            place=middle;
            cout<< "eagle";
            //if matches with user input replace value of place

        }

// if it doesn't find out what side the value falls on
        else if (array[middle]> word)
            last=middle-1;

        else
            first =middle+1;
    }
//decrement
    iCompares--;
    return place;
    //return place to cout
}

>I think that is where my problem is.
I think your messed up main function is where the problem is. Compare your code to this:

int main()
{
  ifstream in ( "test.txt" );

  if ( !in )
    cout<< "There was an error unable to read file!";
  else {
    string totalwords[size];
    string word;
    int result;

    cout<< "Enter the word you wish to search for: ";
    cin >> word;

    int i = 0;

    while ( i < size && getline ( in, totalwords[i++] ) )
      ;

    result = binarySearch ( totalwords, i, word );

    if ( result == -1)
      cout<<"Sorry, that word is not in this file.\n";
    else {
      cout<< word <<" was found\nThe search conducted took "
        << iCompares <<" compares";
    }
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.