-
Partial Word
I've found a couple posts about my question and they have helped but I'm still stuck. For a Boggle game I'm have issues with the part that searches my Hash Table for words. Well, full words i am fine with, its finding words that are partially there and continuing or backing out. Using ifstream, is there a piece of code im not aware of that does this?
I have the logic laid out the best i can for the SearchBoard Function.
Start on 0,0 of a [3][3] board.
save char to string
check to see if string is = word in Hash Table
see if partially there <- not sure of code
-continue searching word or back out and go a different way
-make sure next spot is a valid space on the board
rinse and repeat
putting all words that i find into a Binary Search Tree then printing when finished.
Any help would be appreciated. Thank you
-
Not addressing the problem you are asking about, but I noticed some things that I thought I'd point out:
Code:
void Board::LoadWordList()
{
ifstream fi;
HT Hash;
fi.open("Words.txt");
if(fi.is_open())
{
char line[30];
string strline;
while (!fi.eof())
{
fi.getline(line,30);
strline = line;
Hash.Insert(strline);
}
fi.close();
}
}
You should not be using fi.eof() to control your loop here. The reason is that the end-of-file flag only gets set after you attempt to read past the end of the file. When you read your last line from the file, fi.eof() is still false as you've read up to, but not past, the end of the file. The loop will therefore be executed one extra time even though you've just read and inserted the last line. The last read operation will fail and set eof to true however since you're already in the loop, you'll attempt to insert the same data (data in line will still contain what was read in the last successful read) twice. Whether this is a problem depends on how the Insert call handles the insertion of duplicate data... usually it's something you'd want to avoid/consider before you rely on the container to "handle" it. The correct way to do a loop of this kind is to put the getline call directly in place of the eof test. If the getline call fails, then data could not be read and the loop condition will be false and you won't call Insert at all.
Another issue is that Hash is a local object. Once the function ends, it will be popped from the stack and it's destructor called. The end result is that this function does nothing... it's opens a file and reads from it inserting into some container but at the end of the function the container and it's contents disappears and the function has served no purpose. The same thing happens later in your BoardSearch function. You are creating a local Hash/HT object (which should be empty?) and then you are calling the Find member function on that empty(?) object. ... unless there is some implementation detail of the Hash/HT object I'm not aware of.
I'd also use the string version of the getline function that will eliminate the need for the line variable and the extra conversion step from char* to string though you may have a indeed have a reason for this.
And the explicit close call is not needed (though it doesn't hurt) as the stream's destructor called automatically at the end of the function will close the file.
-
Thank you. That helped me with a couple errors. Before, as i was trying to find words it was telling me my head Node was NULL, so that makes since.