Thread: Partial Word

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    2

    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

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.
    Last edited by hk_mp5kpdw; 10-01-2008 at 06:45 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    2
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  5. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 09:04 PM