compare two files and show matches c++

November 4, 2010 at 22:43:13
Specs: Windows Vista
I'm supposed to use an english dictionary file "dictionary.txt" to compare to an article (another text file), find matches and show a counter of each time that word appears in the article. This is what I have so far:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int functionCounter(int count_N, int number);

int main()
// Create ostream object that writes to file outfile
ofstream fout ("step2out.txt");

//Declare and initialize variables
int count_N=1, number=1;

//Call functionCounter
number = functionCounter(count_N, number);

//Construct array
string *A;
A = new string[number];

//Read the words in the file as strings
ifstream fin ("step2.txt");
for(int i = 0; i < number; i++)
fin >> A[i];

//Tell user the file is written to 'step1out.txt'
cout << "The file 'step2out.txt' is written to your folder." << endl;

return 0;

//FUNCTION HERE--------------------------------------
int functionCounter(int count_N, int number)

//Prompt user to enter the name of the file to be read
{ string fileName;
cout << "Enter name of your dictionary file: ";
cin >> fileName;

//Open the file using the input from the user
ifstream fin (fileName.c_str());

//While the file is open count the number of words and print the file in reverse form
if (fin.is_open())
//Define and initialize variables
int count_N=0;
string n;

//Read in string until file is finished
while(fin >> n)
number= count_N;
return number;

return -1;

I understand that a double for loop is what's needed but I don't know how to implement it.

See More: compare two files and show matches c++

Report •

November 5, 2010 at 08:19:57
A double for loop is just a for loop nested in another for loop.
for (int i = 0; i < 5; ++i) {
  for (int j = 0; j < 5; ++j) {
    std::cout << "i = " << i << " j = " << j << '\n';
  std::cout << "End of 'j' loop\n";
std::cout << "End of 'i' loop\n";

How To Ask Questions The Smart Way

Report •
Related Solutions

Ask Question