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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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);
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
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
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243