Sure, it's better. But not perfect. Not yet.
First, make indentation consistent. Don't indent two spaces somewhere and 4 elsewhere. Choose one and stick with it. Alternatively, use a tab.
Also keep blocks separate; indent them all - each block with an extra "indent":
Code:
if (words1[j] == words2[i])
cout << words1[j] <<" is the same as " << words2[i]<< "\n";
Should be:
Code:
if (words1[j] == words2[i])
cout << words1[j] <<" is the same as " << words2[i]<< "\n";
Code:
while (i < ((int)words2.size()))
{
// ...
i++;
}
This is also wrong. It's important that i++ is one more indent more than the while loop itself because it belongs TO the while loop and is not outside.
So:
Code:
while (i < ((int)words2.size()))
{
// ...
i++;
}
And the while loops can be for loops:
Code:
for (i = 0; i < (int)words2.size(); i++)
{
if (words1[j] == words2[i])
cout << words1[j] <<" is the same as " << words2[i]<< "\n";
else
cout << words1[j] <<" is not the same as " << words2[i]<< "\n";
}
And while we're at it, you could use arrays for this.
Code:
vector<ifstream> vFiles;
vector< vector<string> > vWords;
for (int i = 0; i < 2; i++)
{
vFiles[i].open(/* My filename here */);
vWords.push_back( vector<string>() );
for (int j = 0; j < (int)vWords[i][j].size(); j++)
{
// Compare strings
}
}
Just an example.