1. ## Program flow

Would there be a simpler way to do this?
Code:
```
int i=0 , j=0;
while (i < ((int)words2.size()))
{
{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";
}i++;
}
i = 0; j++;
while (i < ((int)words2.size()))
{
{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";
}i++;
}```
Thanks!

Code:
```
int i=0 , j=0;
while (j < ((int)words1.size()))
{
while (i < ((int)words2.size()))
{
{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";
}i++;
}i=0 ; j++;
}```

3. Code:
```	int i = 0, j = 0;
while ( i < (int)words2.size() )
{
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";
i++;
}
i = 0;
j++;
while ( i < (int)words2.size() )
{
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";
i++;
}```
This doesn't make that much sense. I'm thinking you're trying to compare lots of different words, like words1, words2, words3, etc?
Those while loops are basically for loops to, so you can change that.

4. I suppose the question is: what are you trying to achieve? Are you trying to compare words, or letters, or every element of words1 to words2, or what?

(PS: Doesn't .size() return an int by itself? Why the cast?)

5. Ok thanks, im trying to find the right way to indent, as i didnt like too much the traditional way.
But i guess i will have to stick with it as i cant seem to find better.
Ok i try to post the whole code indented properly.

6. So, again, what is the code trying to do?
Btw, I can give you some tips in indentation here:

Code:
`{if (words1[j] == words2[i])`
This is confusing because you're actually making a local block and putting the start at the same line as the if. Break it into two lines. Typically it's much easier to read if all statements take one line each. Don't put two statements or lines into one.

Code:
`     }i++;`
End of local block, i + 1 (basically for loop), again confusing due to two lines combined into one.

7. Code:
```
#include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <vector>
using namespace std;

int main ()
{
ifstream file1;
ifstream file2;
//open file1
vector<string> words1;
file1.open ("1.txt");
string str1;

while( !file1.eof() )
{
getline (file1,str1);
words1.push_back (str1);
}

//open file2
vector<string> words2;
file2.open ("2.txt");
string str2;

while( !file2.eof() )
{
getline (file2,str2);
words2.push_back (str2);
}
int i=0 , j=0;
while (i < ((int)words2.size()))
{
{
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";
}
i++;
}
i = 0; j++;

while (i < ((int)words2.size()))
{
{
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";
}
i++;
}
file1.close();
file2.close();
system("pause");
return 0;
}```
Thanks, i hope its better.

8. 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.

9. Wow, thanks for your precious time Elysia, thats really nice of you.
Im gonna try to take good note of this in my indentation in the future.

I could use arrays for this?
Im confused, cause you said to me the last time to avoid using arrays , rather using strings instead, being more secure.

10. Ah, but that was referring to char arrays.
char myaray[50];
It's better to use std::string for that.
But there's nothing wrong with
int n[50];
Though if you need a dynamic array, you can use std::vector.

If you know you have only two files, you can write:
Code:
```vector<ifstream> vFiles;
vector<string> vWords[2];
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
}
}```

11. Cool, thank you!

12. vector<ifstream> vFiles;
You can't actually make a vector of streams because they are non-copyable. What you could do, however, is make a function to read a file into a vector of strings:

Code:
```void read_file(const char* name, vector<string>& words)
{
ifstream fin(name);
string line;
words.clear();
while (getline(fin, line)) {
words.push_back(line);
}
}

int main ()
{

vector<string> words1;
vector<string> words2;

...```
Note that file reads shouldn't be controlled with eof(). In fact your program will read the last line twice if you do.

As to the comparison part, you should be able to reduce redundant code using nested loops. Also, if you want to look at each item in a container, it might be easier to use a for loop:
Code:
```for (size_t i = 0; i != words2.size(); ++i) {
//loop body here
}```
Also worth noting: rather than casting the return type of size(), one might use the correct type to compare it against in the first place (size_t i in this snippet).

13. Thank you Anon!

14. So if I'm reading this right, you want to find all the words that appear in both files?

In that case I suggest a number of things:
1. Don't output the words that are different from each other, there will be masses of these compared to those found that are the same, as every word is not the same as almost every other word.
2. Don't use a naive O(n*n) algorithm to find these. It will take increasingly forever as the file sizes increase. A better way to do it is to read in all words from both files and then sort both word lists then iterate through both lists comparing items in much the same way as you'd perform a mergesort. There are std library algorithms to do all this for you though, that make it extremely easy....

First, read all words from each file into a set<string>
Then use std_intersection to get all words that are in both files. Use set_symmetric_difference to find all words in either file that are not in both files if you wish.
Note that this also takes care of any words that appear multiple times in the same file.

Not only is this incredibly easy, it's incredibly fast thanks to the efficient algorithms used by the SC++L!

15. Thank you iMalc for your useful input. I didnt even know about those functions.

Actually this program is just a step toward the final program which is supposed to compare
each word of file1 with each word of file2, that would be a sort of a dictionary and take some
action on words in file1 if they were found in file2.

But i guess this set_intersection funtion was made just for that. Thats really awesome.