C++ comparing words from two files (ifstream)?

I want to find the words from file1 in file2. I have already put the words of file1 into a string delimited by a space. I want to check if the words from file1 is also in file2. However, I am not sure as to what is wrong with my code and I would really appreciate any helpful input. I'm storing the words of file1 into a string, because I'm expecting a small file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
.
.
.
if(file2.is_open()){
	for (int i = 0; i < file1words.length(); i++){
		
		if ((file1words.at(i) >= 65 && file1words.at(i) <=90) || (file1words.at(i) >= 97 && file1words.at(i) <=122)){
			temp3 += file1words.at(i);
		}
		else {
			while(!file2.eof()){
				file2 >> dictWords;

					if(temp3 == dictWords){
						counter++;			//count how many times words from file1 found in file2
						cout << counter << endl;
					}// End of if
			}// End of while loop

			if (counter == 0){
				cout << temp3 + " is not found in file2";

			}
			counter = 0;

			temp3 = "";


			file2.clear();
			file2.seekg(ios::beg);


		}// end of if-else

	}// End of for-statement


}// End of if-statement

else{
	cout<<"File does not exist!"<<endl;
	exit(1);
}// End of if-else

file2.close(); 


There are two problems, aside from others, that I am not sure about;
1. Is the compare statement correct?
2. Can the code reset the stream after the while-loop?
3. After comparing the first word of file1 with file2, it doesn't compare the rest of the words of file1. I did a cout, and it keeps comparing the first word.

Thanks in advance
closed account (28poGNh0)
I hope you look for this one

I assume that you have two text file
a.txt contains
techno01 is wanted


b.txt contains
Hello I am techno01 nice to meet you


both are in c: driver

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# include <iostream>
# include <fstream>
using namespace std;

int main()
{
    ifstream firstFile("c:/a.txt"),secondFile("c:/b.txt");
    string fFileStr,sFileStr;

    if(firstFile&&secondFile)
    {
        while(firstFile)
            fFileStr += (char)firstFile.get();
        while(secondFile)
            sFileStr += (char)secondFile.get();

        cout << "This is the content of the first file " << endl << "\"" << fFileStr << "\"" << endl;
        cout << endl << "Enter word which you look for in file 2 -> " << endl;
        string word;
        getline(cin,word);

        if(sFileStr.find(word)!=string::npos)
            cout << "Word found";
        else
            cout << "Cannot find your word " << endl;

    }else cout << "Cannot open the files " << endl;
}
I really appreciate your help, but I don't need the user to enter a word. Basically, I want to write a spell checker like Microsoft word. Instead of correcting the misspelled/wrong words that were not found in the dictionary, I want to display it to the user. Suppose we have the following two files:

File1.txt:
===================================
I am new to c++. Earth is the center of the universe;-;)-? !2929324
saldkfj032..... Universe$$$ 32@$SAADSF
iwiewu woewi
Hello
Bye By=By3 cannot find the files?!
Word
World
===================================

File2.txt:
===================================
You
are
here
to
speak
chinese
english
Tracking
options
internet
===================================

I have already extracted and separated the words of the first file. The code that I posted is the comparing part, but it's not working. The code that you posted works perfect, if you could modify it for my purpose, it will be excellent. The first file name should be taken from the user.
closed account (28poGNh0)
I did this program once I hope that will help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# include <iostream>
# include <fstream>
using namespace std;

void checkIsWordFound(string wordToFind,string text)
{
    if(text.find(wordToFind)!=string::npos)
        cout << wordToFind;
    else
    for(unsigned int i=0;i<wordToFind.size();i++)
        cout << "?";
}

int main()
{

    ifstream firstFile("c:/a.txt"),secondFile("c:/b.txt");
    string fFileStr,sFileStr;

    if(firstFile&&secondFile)
    {
        while(firstFile)
            fFileStr += (char)firstFile.get();
        while(secondFile)
            sFileStr += (char)secondFile.get();

        cout << "This is the content of the first file " << endl;

        string tmpStr;

        for(unsigned int i=0;i<fFileStr.size();i++)
        {
            if(fFileStr[i]!=' '&&fFileStr[i]!='\n')
                tmpStr += fFileStr[i];
            else
            {
                checkIsWordFound(tmpStr,sFileStr);
                tmpStr = "";
                if(fFileStr[i]==' ')cout << " ";
                if(fFileStr[i]=='\n')cout << endl;
            }
        }

    }else cout << "Cannot open the files " << endl;
}
Thank you very much for your help. I'm still trying to combine your first code with my code, but I can't. I have to implement this without functions and arrays or any vectors or advanced stuff.
I keep forgetting to provide more details. I'm sorry about that.
closed account (28poGNh0)
No progblem man just bring problems we will appreciate it
Topic archived. No new replies allowed.