Vector/while loop help! [urgent]

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2009
Posts: 27
Reputation: scantraXx- has a little shameless behaviour in the past 
Solved Threads: 0
scantraXx- scantraXx- is offline Offline
Light Poster

Vector/while loop help! [urgent]

 
0
  #1
Aug 29th, 2009
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Vector/while loop help! [urgent]

 
-7
  #2
Aug 29th, 2009
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 PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,459
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso

Re: Vector/while loop help! [urgent]

 
0
  #3
Aug 29th, 2009
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 :
  1. ifstream inputFile("toBeOrNot2Be.doc");
  2. inputFile.seekg(0,std::ios_base::beg);
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Vector/while loop help! [urgent]

 
1
  #4
Aug 29th, 2009
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
  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

  1. read the data file, storing to vector of strings
  2. loop
  3. ask user for input word to find
  4. search for word
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 27
Reputation: scantraXx- has a little shameless behaviour in the past 
Solved Threads: 0
scantraXx- scantraXx- is offline Offline
Light Poster

Re: Vector/while loop help! [urgent]

 
0
  #5
Aug 29th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 27
Reputation: scantraXx- has a little shameless behaviour in the past 
Solved Threads: 0
scantraXx- scantraXx- is offline Offline
Light Poster

Re: Vector/while loop help! [urgent]

 
0
  #6
Aug 29th, 2009
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 150
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Re: Vector/while loop help! [urgent]

 
0
  #7
Aug 29th, 2009
Originally Posted by scantraXx- View Post
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Vector/while loop help! [urgent]

 
-7
  #8
Aug 29th, 2009
Originally Posted by scantraXx- View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 27
Reputation: scantraXx- has a little shameless behaviour in the past 
Solved Threads: 0
scantraXx- scantraXx- is offline Offline
Light Poster

Re: Vector/while loop help! [urgent]

 
0
  #9
Aug 29th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,970
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Vector/while loop help! [urgent]

 
0
  #10
Aug 29th, 2009
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)
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 469 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC