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