For this problem, I have to have two text files: one for list of words that need to be found and other containing the document that needs to be search. The results would placed in a output "results" file later on.
What I've Done: I've already figured out a way to store all of the word list onto an array and properly display it in the list.txt file.
Current Problem: How to find words that are the same and to increment the counter every time it is found. I've played around with the code but it still won't work.
Any help would be greatly appreciated!
int main()
{
string wordList[100];
string words;
int wordCount = 0;
inFile1.open("list.txt");
if( inFile1.fail() )
{
cout << "Could not open input file - Aborting program!\n";
exit(1);
}
else
{
for(int i = 0; i < 100; i++)
{
inFile1 >> wordList[i];
}
}
outFile.open("results.txt");
if( outFile.fail() )
{
cout << "Could not open output file - Aborting program!\n";
exit(1);
}
inFile2.open("doc.txt");
if( inFile2.fail() )
{
cout << "Could not open input file - Aborting program!\n";
exit(1);
}
else
{
for (int i = 0; i < 100; i++)
{
while(inFile2 >> words)
{
if(words == wordList[i]) wordCount++;
inFile2 >> words;
}
}
}
outFile << "-----------------------" << endl;
outFile << "Word\t\tCount" << endl;
outFile << "-----------------------" << endl;
for (int i = 0; i < 100; i++)
{
outFile.width(2);
outFile << wordList[i] << "\t\t" << wordCount << endl;;
}
cout << words;
inFile1.close();
inFile2.close(); // Close all files explicitly
outFile.close();
return 0;
}