Nov 7, 2010 at 5:50pm UTC
I'm trying to write a program that reads text from one file and determines which words from that file were found and how many times they were found in another file. Then i need it to output this information to a separate file. My program is compiling but it crashes when I run it. I'm not sure whats wrong with it.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int matchingwords(int count,int countnew,string A,string B);
int main()
{
ofstream fout("step2out.txt");
//intro
cout << "Welcome to MIDN Combado's project #2" << endl;
int count=0,countnew=0;
string words,A,B;
//open dictionary file
ifstream fin("EnglishDictionary.txt");
//count how many words are in file
while(!fin.eof()){
fin >> words;
count++;
//create array A and read strings from file
string *A= new string[count];
for(int i=0;i<count;i++) {
fin >> A[i];
}
}
fin.close();
//open file step2.txt
ifstream in("step2.txt");
//count how many words are in file step2.txt
while(!in.eof()) {
in >> words;
countnew++;
//create array B and read strings from file
string *B=new string[countnew];
for(int j=0;j<countnew;j++) {
in >> B[j];
}
}
in.close();
cout << matchingwords(count,countnew,A,B);
fout << matchingwords(count,countnew,A,B);
return 0;
}
int matchingwords(int count,int countnew, string A, string B)
{
int match=0;
for(int i=0;i<count;i++) {
for(int j=0;j<countnew;j++) {
if(A[i] == B[j]) {
match++;
}
}
}
return match;
}