| | |
Vector/while loop help! [urgent]
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2009
Posts: 27
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
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
Last edited by scantraXx-; Aug 29th, 2009 at 12:22 am.
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.
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 :
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)
ifstream inputFile("toBeOrNot2Be.doc"); 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
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
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
Unless you do
C++ Syntax (Toggle Plain Text)
dict.close( ); dict.clear( );
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)
read the data file, storing to vector of strings loop ask user for input word to find 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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Aug 2009
Posts: 27
Reputation:
Solved Threads: 0
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. 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)
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
Last edited by scantraXx-; Aug 29th, 2009 at 1:44 am.
•
•
Join Date: Apr 2009
Posts: 150
Reputation:
Solved Threads: 7
•
•
•
•
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)
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
we just did that assignment 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.
•
•
•
•
Could you help me out by writing out the psuedocode or give me any hints? I'm trying to attempt it now.
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.
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."
![]() |
Similar Threads
- Starting Python (Python)
- std::vector and for loop questions (C++)
- Suspected Vector Trouble (Java)
- vector subscript out of range (C++)
- Help with looping through a vector (C++)
- How to get the values for checked and unchecked chekboxes (Java)
- Passing Objects (C++)
- I need help writing a program using Fibonacci sequence (C++)
Other Threads in the C++ Forum
- Previous Thread: Read from txt file into struct array
- Next Thread: Fraction problem
Views: 469 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






