I am trying to write a "censor" program. here are the instructions:
Write a program that reads a file of text and replaces each `target'
word with its first letter followed by a string of asterisks that indicate
its length. Have it read the list of target words from a file, so that new
words to censor can be added. Allow words to be separated by newlines,
blanks, tabs, formfeeds, and punctuation symbols like commas, periods, single
and double quotes, and parentheses.
And here's what I've got so far:
#include <string>
#include <fstream>
#include <vector>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <iterator>
using namespace std;
int words();
void censor(const vector<string>& x, int);
void write_to_file( const vector<string>& message);
int main(){
words();
system ("pause");
}
int words (){
int count =0;
std::vector<std::string> file;
std::string line;
file.clear();
std::ifstream infile ("words.txt", std::ios_base::in);
while (getline(infile, line, '\n'))
{
file.push_back (line);
count ++;
}
censor(file, count);
}
void censor(const vector<string>& inFile, int count)
{
int messagecount =0;
std::vector<std::string> message;
std::string messageword;
message.clear();
std::ifstream infile ("myfile.txt", std::ios_base::in);
while (getline(infile, messageword, '\n'))
{
message.push_back (messageword);
messagecount++;
}
for (int k=0; k < messagecount; k++)
{
for (int h=0;h < count ; h++)
{
if (inFile[h]== message[k])
{
message[k] = "******";
}
}
}
cout << "calling write\n ";
write_to_file(message);
}
void write_to_file(const vector<string>& message)
{
std::fstream messageFile ("myfile.txt", std::ios_base::out);
for (int y=0; y < message.size() ; y++ )
{
messageFile << message[y] << ' ';
}
}
So my problem is with "(getline(infile, messageword, '\n'))". Is there any way to have more than one delimiter in this? To allow for "newlines, blanks, tabs, formfeeds, and punctuation symbols like commas, periods, single and double quotes, and parentheses"? I was just trying to get the basics before worrying about all that. I tried putting two conditions in the line, something like
"(getline(infile, messageword, '\n' || ' '))", but that doesn't work.