I am trying to write a "censor" program that will use a "message" file and a "words" file that contains the banned words. It should replace all the banned words with the the first letter followed by asterisks to indicate the word's length. I am having trouble trying to get the punctuation and spaces seperate from the words. here's what I've got:
Code:
#include <string>
#include <fstream>
#include <vector>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
char ch;
string word="";
ifstream messagefile("myfile.txt");
ofstream out("outfile.txt");
int bannedcount =0;
std::vector<std::string> bannedfile;
std::string line;
bannedfile.clear();
std::ifstream infile ("words.txt", std::ios_base::in);
while (infile >> line)
{
bannedfile.push_back (line);
bannedcount ++;
}
while( messagefile.get(ch) )
{
do
{
word += ch;
messagefile.get(ch);
}
while ( !ispunct(ch) && !isspace(ch));
for (int h=0; h < bannedcount ; h++)
{
if (word== bannedfile[h])
{
cout <<"Changing " << bannedfile[h] << endl;
for(int i = 1; i < bannedfile[h].length(); i++)
{
word[i] = '*';
}
}
}
out << word;
word=ch;
string spaces="";
do
{
messagefile.get(ch);
spaces =+ ch;
}
while ( ispunct(ch) || isspace(ch));
out << spaces;
spaces.clear();
}
messagefile.close();
out.close();
system("del myfile.txt");
int result;
char oldname[] ="outfile.txt";
char newname[] ="myfile.txt";
result= rename( oldname , newname );
if ( result == 0 )
puts ( "File successfully renamed" );
else
perror( "Error renaming file" );
system ("pause");
}
Anyone see the problem?