Write a program to generate personalized junk mail. The program takes input both
ID: 3552460 • Letter: W
Question
Write a program to generate personalized junk mail. The program takes input both from an input file and from the keyboard. The input file contains the text of a letter except the name of the recipient is indicated by the three characters #N#. The program asks the user for a name and then writes the letter to a second file but with the three letters #N# replaced by the name. The three-letter string #N# will occur exactly once in the letter.(Will occur many times in our program) Hint: Have your program read from the input file until it encounters the three characters #N#, and have it copy what it reads to the output file as it goes. When it encounters the three letters #N#, it then sends output to the screen asking for the name from the keyboard. You should be able to figure out the rest of the details. Your program should define a function that is called with the input and output streams as arguments. Additional requirements: We are going to allow the three letters #N# occur many times in the letter. All occurrences should be replaced with the recipientExplanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
void replace(ifstream&,ofstream&);
int main()
{char filename[30];
bool found=false;
cout<<"what is the name of the input file youare using? ";
cin>>filename;
ifstream input;
ofstream output;
input.open(filename);
if(input.fail())
{ cout<<"file did notopen please check it ";
system("pause");
return 1;
}
cout<<"what is the name of theoutput file you are using? ";
cin>>filename;
cin.ignore(100,' ');
output.open(filename);
replace(input,output);
input.close();
output.close();
system("pause");
return 0;
}
void replace(ifstream& in,ofstream& out)
{bool found=false;
string lookfor="#N#";
string data,name;
int n;
getline(in,data);
while(in)
{if(!found)
{n=data.find(lookfor,0);
if(n!=-1)
{found=true;
cout<<"Enter name: ";
getline(cin,name);
data.replace(n,3,name);
}
}
out<<data<<endlu;
getline(in,data);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.