I've trying it with a loop but it count the first word but it didn't count the rest of the words.
so I tried manipulating the strings by filling in arrays of string of each word of the text and putting a space in each word but that didn't work to :crying:
Here's the program
//John Nguyen
//This program reads and prints out a text line by line
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
const int MAX = 50;
void diffwords(string, ifstream &infile, ofstream &outfile);
int read(string[],int &,ofstream &outfile);
void change(string &);
int main()
{
ifstream infile("input.txt");
//ifstream infile("con");
//ofstream outfile("outfile.txt");
ofstream outfile("con");
string line[MAX];
int i;
read(line,i,outfile);
for(int count = 0; count < i; count++)
{ change(line[count+1]);
diffwords(line[count],infile,outfile);
}
}
void diffwords(string str, ifstream &infile,ofstream &outfile)
{ string text;
getline(infile,text);
outfile << text << endl;
int count = 0;
string::size_type pos;
pos = text.find(str,0);
while(pos != string::npos)
{
count++;
pos = text.find(str,pos+str.size());
}
outfile << "the word " << str << " has appeared " << count << " times" << endl;
}
int read(string str[], int &i,ofstream &outfile)
{
i = 0;
ifstream infile("input.txt");
//ifstream infile("con");
while(infile >> str[i])
{
outfile << str[i] << endl;
i++;
}
return i;
}
void change(string &str)
{
str.insert(0," ")
}
Here's the output too
Quote
The
pie
is
really
really
good
The pie is really really good
the word The has appeared 1 times
the word pie has appeared 0 times
the word is has appeared 0 times
the word really has appeared 0 times
the word really has appeared 0 times
the word good has appeared 0 times
Process returned 0 (0x0) execution time : 0.328 s
Press any key to continue.
pie
is
really
really
good
The pie is really really good
the word The has appeared 1 times
the word pie has appeared 0 times
the word is has appeared 0 times
the word really has appeared 0 times
the word really has appeared 0 times
the word good has appeared 0 times
Process returned 0 (0x0) execution time : 0.328 s
Press any key to continue.
please help


Sign In
Create Account

Back to top









