![]() |
| ||
| Search for a word in a dictionary Hi all. I'm trying to write a "simple" program for a word game (something somewhat similar to Scrabble) and I got stopped by the task I tought to be easy enough to start with. I have a text file (Italiano.list) wich is a collection of italian words listed alphabetically as below: a What I would like it to do is to let the user input a string (word) and then search for that word in the "dictionary". Obviously it should also tell if the word is present or not. Here's what I wrote so far: it seems to me to be logical and quite simple but evidently there's something I am missing... I think it opens correctly the file and it stores as expected each line in the string current_word (substituting the 2nd if with a simple "cout << current_word << endl;" makes it print the whole dictionary on screen) but then it fails in comparison because it always state that the word is not present... even if that particular word actually IS present! Also at line 21 there is another curious error... comment should explain it. Btw, thanks in advance for every hint or try you'll eventually give to me and sorry as always for my english. #include <iostream> Edit: here's the link to where I learned that tiny little bit of handling files I've tried to use in this program: http://www.cplusplus.com/doc/tutorial/files.html |
| ||
| Re: Search for a word in a dictionary I can't see why this program shouldn't work. The only thing I would change is: Change these to lines to: while(getline(dictionary, current_word)) { eof() is never a good function to use IMO. |
| ||
| Re: Search for a word in a dictionary Thanks for the advice. I made the change you suggested (I found it more beautiful, thx!) but I couldn't get any improvement so far... Could the problem be within the file? The word I searched for was "abaco" and it's formatted exactly as shown in the previous post (I copy-pasted it) ... Don't know if it's relevant (and I think it's not) but just to be complete let me say that I got 26 separate files (one for each alphabet letter) that I unified in one with "cat file1 file2 ... file26 Italiano.list" |
| ||
| Re: Search for a word in a dictionary Your code works fine with a dummyfil I created to test it myself. So the problem would indeed be the list-file. Linux has different 'line endings' then for example windows. So you might want to try typing unixtodos Italiano.listin a console |
| ||
| Re: Search for a word in a dictionary I'm sure you are right, there's a problem with line endings. If I insert an empty line immediately after a chosen word and then search for that word it works fine. Anyway, neither flip -u Italiano.list(wich is supposed to convert line endings to unix specs) nor (expectedly) flip -m Italiano.list(wich on the contrary sets them to dos type) works. I downloaded the file from http://http://www.yorku.ca/lbianchi/...rds/index.html (This wordlist is Copyright © 1993-2002 Luigi M Bianchi, and is made freely available under the terms of the GNU General Public License. For further information concerning this license, please visit the GNU Project Web Server.). If I can't think of anything better I'll end up inserting an empty line after each word of the list ç_ç However, thanks for all the help, it was greatly appreciated :) EDIT: fromdos Italiano.listworked perfectly! Now there are no more problems, it finds every word just as expected :) 1000 Thanks and sorry for having disturbed with such a ridiculous problem (it wasn't even the right forum to post that, after all :$ ) |
| All times are GMT -4. The time now is 9:29 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC