Nov 7, 2010 at 4:45pm 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;
}
Nov 8, 2010 at 1:10pm UTC
It looks like you want is "string array" rather than "string"
1 2 3
int matchingwords(int count,int countnew, string A, string B);
to
int matchingwords(int count,int countnew, string* A, string* B);
and in main() function:
1 2 3
string words,A,B;
to
string words,*A,*B;
and,
1 2 3
string *A=new string[count];string *B=new string[countnew];
to
A=new string[count];B=new string[countnew];
finaly, you must release point array A and B.
Last edited on Nov 8, 2010 at 1:12pm UTC
Nov 26, 2010 at 4:56pm UTC
I've been working on a similar project , and ive used this code actually , it helped me , but i have some sort of a problem i cant recognize....so can anyone please run the following code and tellme how to solve the problem :
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int matchingwords(int count,int countnew,string A,string B);
void main()
{
ofstream write("output.txt");
int count=0,countnew=0;
string words,*A,*B;
ifstream read("input.txt");
while(!read.eof())
{
read >> words;
count++;
A=new string[count]; //create array A and read strings from file
for(int i=0;i<count;i++)
{
read >> A[i];
}
}
read.close();
ifstream in("input2.txt");
while(!in.eof())
{
in >> words;
countnew++;
B=new string[countnew]; //create array B and read strings from file
for(int j=0;j<countnew;j++)
{
in >> B[j];
}
}
in.close();
cout << matchingwords(count,countnew,*A,*B);
write << matchingwords(count,countnew,*A,*B);
}//end of main
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;
}