if (user_input == 1)
          {
        	  dict.open("dict.txt");

        	  cout << "Enter word ";
        	  cin >> word_check;
        	  toLower(word_check);

        	  vector<string> lines;
        	  string line;

        	  if (dict.is_open()) // if the dictionary is open
        	  {

        		  while (getline(dict,line))
        		  {
            		  lines.push_back(line);
        		  }

        		  if (binarySearch(lines, lines.size(), word_check) != -1)
        		  {
        				  cout << word_check << " is in dictionary" << endl;
        		  }
        		  else
        		  {
        				  cout << word_check << " is not in dictionary" << endl;
        		  }

        		  cout << lines.size() << endl; // testing to see if vector has stored all words
        	  }
          }

Okay this is how the program should work:

1. Opens the dictionary file
2. Reads all words (lines) from dictionary file into the vector "lines" and SHOULD store all the words in the vector
3. Uses a binary search to search for specific words STORED IN THE VECTOR and display if found or not..

The problem is.. on the first around, all words are read correctly and the word is able to be found.. but second time around none of the words are read and no words can be found..

Help please, this is really frustrating.

Thanks :)

Recommended Answers

All 9 Replies

It probably doesn't work the second (and subsequent) time around because the file isn't closed, so it can not be reopened. And why would you want to read that file over and over and over .. and over every time you select option #1??? Just read it upon program start and don't worry about it any more.

don't know or don't care on why you are reading the same file
twice, if i understand correctly, but to answer your question,

reset the file pointer to the beginning after reaching the eof(), by
using similar to this code :

ifstream inputFile("toBeOrNot2Be.doc");
inputFile.seekg(0,std::ios_base::beg);

If the code above is in a loop, where you ask for a new word to seek, your problem is that the file is left pointing past its end.

Unless you do

dict.close( );
dict.clear( );

before you open the file again, you will not be rereading the file.

More to the point, why reread it? That takes considerable time, relatively speaking. Read and store the file outside any loop, then the data is always available during the run of the program.

And, declaring the string vector inside the loop creates a new instance, which is empty. So your non-reading of the data is going into an empty vector, leaving it empty.

Better overall structure

read the data file, storing to vector of strings
loop
    ask user for input word to find
    search for word
commented: You're a very good teacher. And this post is as solid as all the other ones you've made, keep up the good work! +21

EDIT: Fixed it, thanks vmanes.

The problem was that my vector was declared inside the if loop lol.

Much appreciated.

Okay I need help with this second part please.

1. Find words in dictionary which match pattern
The user is prompted to enter a word pattern which contains ? for missing letters, the word pattern is converted to lower case and the program displays words in array which match pattern. The words are displayed in alphabetical order. See required prompts and output in the sample run.


Can anyone generate the code for me according to the information and code I posted above?

Thanks.

EDIT:
This is what it should look like:

Crossword Helper 

 1 : Check if word is in dictionary 
 2 : Find words in dictionary which match pattern 
 3 : Quit 

2 
Enter word pattern with ? for missing letters 

y??ht?w????

yachtswoman

Okay I need help with this second part please.

1. Find words in dictionary which match pattern
The user is prompted to enter a word pattern which contains ? for missing letters, the word pattern is converted to lower case and the program displays words in array which match pattern. The words are displayed in alphabetical order. See required prompts and output in the sample run.


Can anyone generate the code for me according to the information and code I posted above?

Thanks.

EDIT:
This is what it should look like:

Crossword Helper 

 1 : Check if word is in dictionary 
 2 : Find words in dictionary which match pattern 
 3 : Quit 

2 
Enter word pattern with ? for missing letters 

y??ht?w????

yachtswoman

Do you go to Mq Uni? :) we just did that assignment

Can anyone generate the code for me according to the information and code I posted above?

Yes I could, but I won't. Post your attempts to solve it.

Okay fair enough.

Could you help me out by writing out the psuedocode or give me any hints? I'm trying to attempt it now.

Thanks.

Could you help me out by writing out the psuedocode or give me any hints? I'm trying to attempt it now.

The most important thing you should have in the program (and which you should write first IMO), is a function which recognizes a pattern in a word, start off by making such a function.
You could do something like this:

  • Your function receives two strings as argument, and returns a value of type bool.
  • If both strings are not of the same length, then it can never be a pattern, so return false.
  • Otherwise loop through both strings, character by character, if the character is not a '?', then you check whether they're the same, if not, then return false.
  • If the loop has completed (and the function hasn't exited yet because of a non-matching character), you just return true.

And now you can start writing the code.
Please post your finished code down, so we can see the effort you made.

Edit::
You could also write your function in such a way that it accepts a character which it treats as the 'pattern character', for example '*' instead of '?', for this purpose you'll have to pass the function a third argument, (or you could hard-code all the supported 'pattern characters'), but I'm not going any further in depth on this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.