Hey. I'm trying to read strings into an array from a file that contains a list of words. This is so that I can check to see if strings are a real word by seing is they exist inside my array. I have everything working except the compare. My binary search even passes by the word in question. When it compares the two words which are exactly the same, it still returns false. I think the problem is probably in the way I am pulling the words in because the string.compare() function works fine normally. Here is that code. I would love some help. Thanks.
ifstream dictFile;
dictFile.open("dictionary.txt");
if (!dictFile) // testing if file open
{
cout << "Error opening dictionary file" << endl;
}
int index = 0; // dictionary must progress start at line 1
while(!dictFile.eof())
{
getline(dictFile,dictionary[index]);
index++;
}
dictFile.close();
Is there anything just plain wrong about how I am doing this?
EDIT Here is the comparison code as well
bool database::is_word(string word)
{
int ii;
int comp;
int min = 0;
int max = dictSize;
// this will go into the dictionary and look for the word
// it uses a binary search pattern
while (min<=max)
{
ii = (min+max)/2;
comp = word.compare(dictionary[ii]);
cout <<dictionary[ii];
if (comp==0)
{
cout << word<< " is a word!" << endl;
return 1;
}
else if (comp < 0)
{
max = ii-1;
}
else
{
min = ii+1;
}
}
cout << word << " is NOT a word!" << endl;
return 0;
}
is_wordand are you sure that yourdictSizevariable is valid? I just wrote some quick test code and it works perfectly for me.