|
-
May 6th, 2008, 04:04 PM
#1
loop issues
i want to go threw a list of words and find if there is a string thats equal to another list. But my loop only finds one word of the four that it should. any help would be much appriciated.
ifstream fin;
fin.open ( "Wordlist.txt" );
ifstream fin2;
fin2.open ( "Words.txt" );
string line;
string line2;
while ( getline ( fin2, line2 ) )
{
while ( getline ( fin, line ) )
{
if ( line == line2 )
{
cout << "Found word that matched: " << line << endl;
}
}
}
-
May 6th, 2008, 04:42 PM
#2
Re: loop issues
The problem is that your code is set up to go through the entire Wordlist.txt file for each line in Words.txt. After the first time through, you don't reset the file stream to point to the beginning again. To solve this, you could declare your fin ifstream inside the first while loop, or you could clear() the stream and reposition the file pointer to the beginning.
However, your algorithm isn't a really good one because it reads the same file over and over. You should consider coming up with another solution. Perhaps by storing the words in the Words.txt file and then going through the Wordlist.txt one line at a time (or vice versa). how you do that depends on how much C++ you know and how much you're allowed to use.
-
May 7th, 2008, 02:15 AM
#3
Re: loop issues
 Originally Posted by DDCO
i want to go threw a list of words and find if there is a string thats equal to another list. But my loop only finds one word of the four that it should. any help would be much appriciated.
ifstream fin;
fin.open ( "Wordlist.txt" );
ifstream fin2;
fin2.open ( "Words.txt" );
string line;
string line2;
while ( getline ( fin2, line2 ) )
{
while ( getline ( fin, line ) )
{
if ( line == line2 )
{
cout << "Found word that matched: " << line << endl;
}
}
}
Can you provide an example? I'm not sure I understand it. Do you want to check whether a line in a file contains all the words in a list?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|