C++ coding, using string and vector Specifications (overview) 1. Start by readin
ID: 3690480 • Letter: C
Question
C++ coding, using string and vector
Specifications (overview)
1. Start by reading the message from the message file. Make sure to preserve newlines and other whitespace formatting. Store the message in a string variable called message.
2. Next, read the names from the names file. Store each name as an element in a vector of strings called names.
3. Ask the user what name to use for the sender. Store the response in a string called sender. The name the user enters might contain spaces.
4. Allow the user to enter a search term (eg James) and search the name vector for any name that matches. Store all matching names in a vector called recipients. These will be the people the message gets addressed to.
5. In an output file, create unique versions of the message addressed to each recipient by replacing “<recipient name>” with the names in the recipients vector. Make sure the sender’s name is used instead of “<sender name>”. Demarcate each unique version of the message in the file with a couple of newlines and a line of equal signs.
Specifications (specific)
Assume that the message file will contain the string “<recipient name>” in any location that the recipient’s name should go. Assume that the message file will contain the string “<sender name>” anywhere that the sender’s name should go. Either strings may occur more than once.
Implement a function to find names in the name vector that match the query and returns a recipients vector. Take a vector of strings containing names and a string containing a query and return a vector of strings containing the names who contain the query. Assume that case is important (“Thomas” != “thomas”).
Implement a void function that outputs the message. As arguments, take a string for the message, a string for the recipient name, a string for the sender name, and a pass-byreference ofstream for the file you’re writing to.
Sample Output
Assume the following is the contents of the message file:
Dear <recipient name> ,
This is a sample message to confirm that the emailing system works. Please confirm that it does.
Sincerely,
<sender name>
Then the corresponding output would be (user entered text in italics):
What is the name of the sender? Theodore Lindsey
What group of names would you like to send to (enter a search term)? Thomas ==========================================
Dear Thomas Jefferson,
This is a sample message to confirm that the emailing system works. Please confirm that it does.
Sincerely,
Theodore Lindsey
==========================================
Dear Thomas Hendricks,
This is a sample message to confirm that the emailing system works. Please confirm that it does.
Sincerely,
Theodore Lindsey
==========================================
Dear Thomas R. Marshall,
This is a sample message to confirm that the emailing system works. Please confirm that it does.
Sincerely,
Theodore Lindsey
==========================================
In message txt:
In names txt:
Explanation / Answer
#include <iostream>
#include <fstream>
#include <vector>
#include <string.h>
using namespace std;
bool isSubSequence(string str1, string str2, int m, int n)
{
// Base Cases
if (m == 0) return true;
if (n == 0) return false;
// If last characters of two strings are matching
if (str1[m-1] == str2[n-1])
return isSubSequence(str1, str2, m-1, n-1);
// If last characters are not matching
return isSubSequence(str1, str2, m, n-1);
}
void find(vector<string> &recipients, vector<string> names, string recipient)
{
recipient = recipient + " ";
for (int i = 0; i < names.size(); ++i)
{
if(isSubSequence(recipient, names[i], recipient.size(), names[i].size()))
recipients.push_back(names[i]);
}
}
void writeToFile(ofstream &outputFile, vector<string> recipients, string sender, string message)
{
const char * pch;
for (int i = 0; i < recipients.size(); ++i)
{
//const char *c = "sample";
//pch = strstr (message.c_str(),"<recipient name>");
//strncpy (message,c,6);
// message.replace(message.find("<recipient name> "), recipients[i].size()-1, recipients[i]);
size_t index = 0;
index = message.find("<recipient name>", index);
message.replace(index, recipients[i].size(), recipients[i]);
index += recipients[i].size();
index = message.find("<sender name>", index);
message.replace(index, sender.size(), sender);
cout <<message <<endl;
index += sender.size();
outputFile << message << endl; // output message infile
}
}
int main()
{
fstream f("message.txt", fstream::in );
string message;
getline( f, message, '');
f.close();
vector<string> names;
string name;
ifstream file_("names.txt"); // opening names.txt
while(getline(file_,name)) names.push_back(name);
file_.close(); // closing names.txt
string sender;
cout << "What is the name of the sender? ";
getline(cin,sender);
string recipient;
cout << "What group of names would you like to send to (enter a search term)? ";
cin >> recipient;
vector<string> recipients;
find(recipients,names,recipient);
ofstream file("BTP"); // opening file recipient.txt
writeToFile(file,recipients,sender,message);
file.close(); // closing recipient.txt
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.