I am writing a program that reads in from a file (60,000) words. I think I got my code figured out except for the getline part. I will show you what I have ...what am i doing wrong with the getline part?

[code=c++]

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

int binarySearch(string [], int, string);
const int size=60000;
int iCompares=0;


int main ()
{
    string totalwords[size];
    int result;
    string word;


    fstream SortedBinary;

    SortedBinary.open("SortedBinary.txt");
    if (!SortedBinary)
    {
        //Error
        cout<< "There was an error unable to read file!";
        return -1;
    }

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

//I thought this was the correct format but it doesn't work
    //getline(SortedBinary, size, " ")
            result=binarySearch( totalwords,size, word);

            if (result==-1)
                {
                  cout<< "Sorry, that word is not in this file. ";
                }
            else
            {
                cout<< word << " was found";
                cout<< "This search conducted" << iCompares << "compares";
            }


   return 0;
}

int binarySearch(string array [], int element, string value)
{
    int first=0;
    int last = element-1;
    int middle,
    position=-1;
    bool found=false;

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

        if(array[middle] ==value)
        {
            found=true;
            position=middle;
        }

        else if (array[middle]>value)
            last=middle-1;
        else
            first =middle+1;
    }

    iCompares--;
    return position;
}

Forgot to mention that the file that I am reading from is just words with spaces between them..hence the delimiter being " ". thanks!

Here is a link that mentions how the fstream getline works Link

What you need to do is SortedBinary.getline(myBuffer, mySize," "); where myBuffer is a string where you store the word you are reading from the file, and mySize is the maximum size you can store.

I found my way around that getline issue. HOwever, I have what i think is a logic error. I have narrowed it down to a problem in my function. Can you give me a hint to what I am doing wrong? I revised my code to test my loops for a smaller file. The smaller file contains:

apple book cat dog elephant fox giant happy ice joker (spaces in between)

[code=c++]

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

int binarySearch(string [], int, string);
const int size=60000;
int iCompares=0;
char choice;

int main ()
{
    string totalwords[size];
    int result;
    string word;


    fstream sample6;

    sample6.open("sample6.txt");
    if (!sample6)
    {
        //Error
        cout<< "There was an error unable to read file!";

    }

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

    result= binarySearch( totalwords,size, word);

    int CurrentLine = 0;

    while(!sample6.eof())

    getline(sample6, totalwords[CurrentLine++]);

            if (result== -1)
                {
                  cout<< "Sorry, that word is not in this file. ";
                }
            else
            {
                cout<< word << " was found"<< endl;
                cout<< "The search conducted took ";
                cout << iCompares << " compares";
            }


   return 0;
}

int binarySearch(string array [], int element, string value)
{
    int first=0;
    int last = element-1;
    int middle;
    int place =-1;
    bool found=false;

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

        if(array[middle] == value)
        {
            found=true;
            place=middle;
        }

        else if (array[middle]> value)
            last=middle-1;
        else
            first =middle+1;
    }

    iCompares--;
    return place;
}

Any help would be appreciated!

You are searching for a word before you have read in any words from the file. Your call to binarySearch is on line 32. You only start to read from the file on line 36. By line 36, your "search" is complete and you have already decided that the word you were looking for is not in the file. I don't know whether the binarySearch function is correct, but even if it is, you can't find a word in the array if you are searching the array before you put the words into the array. So your main problem is that your binarySearch call needs to be after all the data is read in, not before.

You also shouldn't be using getline if you have more than one word per line and the words are separated by spaces. Just use the >> operator.

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.