943,865 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 875
  • C++ RSS
Aug 29th, 2009
0

Vector/while loop help! [urgent]

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. if (user_input == 1)
  2. {
  3. dict.open("dict.txt");
  4.  
  5. cout << "Enter word ";
  6. cin >> word_check;
  7. toLower(word_check);
  8.  
  9. vector<string> lines;
  10. string line;
  11.  
  12. if (dict.is_open()) // if the dictionary is open
  13. {
  14.  
  15. while (getline(dict,line))
  16. {
  17. lines.push_back(line);
  18. }
  19.  
  20. if (binarySearch(lines, lines.size(), word_check) != -1)
  21. {
  22. cout << word_check << " is in dictionary" << endl;
  23. }
  24. else
  25. {
  26. cout << word_check << " is not in dictionary" << endl;
  27. }
  28.  
  29. cout << lines.size() << endl; // testing to see if vector has stored all words
  30. }
  31. }



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
Last edited by scantraXx-; Aug 29th, 2009 at 12:22 am.
Similar Threads
Reputation Points: 1
Solved Threads: 0
Light Poster
scantraXx- is offline Offline
27 posts
since Aug 2009
Aug 29th, 2009
-7

Re: Vector/while loop help! [urgent]

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Aug 29th, 2009
0

Re: Vector/while loop help! [urgent]

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 :
C++ Syntax (Toggle Plain Text)
  1. ifstream inputFile("toBeOrNot2Be.doc");
  2. inputFile.seekg(0,std::ios_base::beg);
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 29th, 2009
1

Re: Vector/while loop help! [urgent]

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
C++ Syntax (Toggle Plain Text)
  1. dict.close( );
  2. 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

C++ Syntax (Toggle Plain Text)
  1. read the data file, storing to vector of strings
  2. loop
  3. ask user for input word to find
  4. search for word
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Aug 29th, 2009
0

Re: Vector/while loop help! [urgent]

EDIT: Fixed it, thanks vmanes.

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

Much appreciated.
Last edited by scantraXx-; Aug 29th, 2009 at 1:40 am.
Reputation Points: 1
Solved Threads: 0
Light Poster
scantraXx- is offline Offline
27 posts
since Aug 2009
Aug 29th, 2009
0

Re: Vector/while loop help! [urgent]

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:
C++ Syntax (Toggle Plain Text)
  1. Crossword Helper
  2.  
  3. 1 : Check if word is in dictionary
  4. 2 : Find words in dictionary which match pattern
  5. 3 : Quit
  6.  
  7. 2
  8. Enter word pattern with ? for missing letters
  9.  
  10. y??ht?w????
  11.  
  12. yachtswoman
Last edited by scantraXx-; Aug 29th, 2009 at 1:44 am.
Reputation Points: 1
Solved Threads: 0
Light Poster
scantraXx- is offline Offline
27 posts
since Aug 2009
Aug 29th, 2009
0

Re: Vector/while loop help! [urgent]

Click to Expand / Collapse  Quote originally posted by scantraXx- ...
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:
C++ Syntax (Toggle Plain Text)
  1. Crossword Helper
  2.  
  3. 1 : Check if word is in dictionary
  4. 2 : Find words in dictionary which match pattern
  5. 3 : Quit
  6.  
  7. 2
  8. Enter word pattern with ? for missing letters
  9.  
  10. y??ht?w????
  11.  
  12. yachtswoman
Do you go to Mq Uni? we just did that assignment
Reputation Points: 10
Solved Threads: 7
Junior Poster
gretty is offline Offline
158 posts
since Apr 2009
Aug 29th, 2009
-7

Re: Vector/while loop help! [urgent]

Click to Expand / Collapse  Quote originally posted by scantraXx- ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Aug 29th, 2009
0

Re: Vector/while loop help! [urgent]

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.
Reputation Points: 1
Solved Threads: 0
Light Poster
scantraXx- is offline Offline
27 posts
since Aug 2009
Aug 29th, 2009
0

Re: Vector/while loop help! [urgent]

Quote ...
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.
Last edited by tux4life; Aug 29th, 2009 at 10:34 am. Reason: add bullet points (list)
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009

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: Read from txt file into struct array
Next Thread in C++ Forum Timeline: Fraction problem





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


Follow us on Twitter


© 2011 DaniWeb® LLC