943,579 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2275
  • C++ RSS
Jun 30th, 2008
0

Search for a word in a dictionary

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  1. a
  2. ab
  3. abaca
  4. abache
  5. abachi
  6. abacista
  7. abaciste
  8. abacisti
  9. abaco
  10. ...

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.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main(void) {
  7. string key;
  8. string current_word;
  9. ifstream dictionary("Italiano.list");
  10. cout << "Quale parola si vuole cercare?" << endl; // asks for a key
  11. cin >> key;
  12. cout << "La parola che si sta cercando e' la seguente: " << key << endl; // prints the key to checkif it's typed correctly
  13. if(!dictionary.is_open()) {
  14. cout << "Errore. Impossibile accedere al dizionario." << endl; // exits with error for not being able to open the dictionary
  15. return 1;
  16. }
  17. while(!dictionary.eof()) {
  18. getline(dictionary, current_word); // stores the current line of the dic in the current_word string...
  19. if(current_word==key) { // ... and then compares it with the user input
  20. cout << current_word << endl;
  21. cout << "La parola " << current_word << " e' uguale alla parola cercata." << endl; /* line 21: I wanted it to print the string stored in current_word in the case of matching with the user input for another check but it prints actually only " " e' uguale alla parola cercata." << endl; " ! Don't know why, because in the previous line it prints correctly the string current_word... */
  22. cout << "La parola cercata e' presente nel dizionario." << endl; // tells that the word searched for has been found in the dictionary
  23. dictionary.close();
  24. return 0;
  25. }
  26. }
  27. dictionary.close();
  28. cout << "La parola cercata non e' presente nel dizionario." << endl; // tells that the word searched for has not been found in the dictionary.
  29. return 0;
  30. }

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
Last edited by mrboolf; Jun 30th, 2008 at 3:46 am.
Similar Threads
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Jun 30th, 2008
0

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:
  
cpp Syntax (Toggle Plain Text)
  1. while(!dictionary.eof()) {
  2. getline(dictionary, current_word); // stores the current line

Change these to lines to:

cpp Syntax (Toggle Plain Text)
  1. while(getline(dictionary, current_word)) {

eof() is never a good function to use IMO.
Last edited by Nick Evan; Jun 30th, 2008 at 3:56 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jun 30th, 2008
0

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"
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Jun 30th, 2008
0

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.list in a console
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jun 30th, 2008
0

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.list worked 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 )
Last edited by mrboolf; Jun 30th, 2008 at 4:39 am. Reason: Solved!
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Pairs in random numbers
Next Thread in C++ Forum Timeline: error C2677: binary '==' : no global operator found





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC