
Amarya W.
asked 02/26/21What is the code suppose to look like using the inputs below to generate output listing the full names as "LastName, FirstName" with each full name being displayed on a new line?
In this assignment, you are to take in input from two files: one file containing a list of five first names and one file containing a list of five last names. Using this input, you are to generate output listing the full names as "LastName, FirstName" with each full name being displayed on a new line as shown in the example files below. The first name will be on the same line number as the corresponding last name.
What is the code suppose to look like using the inputs below to generate output listing the full names as "LastName, FirstName" with each full name being displayed on a new line?
lastName.txt are Donnelly, Nunez, Peusch, Jackson, Murali
firstName.txt are Quintin, Justin, Drew, William, Beddhu
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{string f1, f2, f3, f4, f5;
string l1, l2, l3, l4, l5;
ifstream infile1;
ifstream infile2;
ofstream outfile1;
infile1.open("lastNames.txt");
infile1 >> l1 >> l2 >> l3 >> l4 >> l5;
infile2.open("firstNames.txt");
infile2 >> f1 >> f2 >> f3 >> f4 >> f5;
outfile1.open("lastNameFirstName.txt");
outfile1 << l1 << "," << f1 << "\n" << l2 << "," << f2 << "\n" << l3 << "," << f3 << "\n" << l4 << "," << f4 << "\n" << l5 << "," << f5 << "\n";
infile1.close();
infile2.close();
outfile1.close();
cout << "loading..." << endl;
system("pause");
return 0;}
1 Expert Answer

Patrick B. answered 02/27/21
Math and computer tutor/teacher
easier said than done...............
How do you know there are only 5 strings in the file? There could be 10,12,... any number...
you have to read until end of file...
Assuming the commas are the required delimiters, you then have to PARSE the buffer so as to
split each name into it's own string. Most other languages like Java have a SPLIT method, but
not the std::string. This MUST be done at the BYTE level, as std::string object cannot be changed.
This is done in the StringSplit routines. You can move them into their own source code file
if you wish
---------------------------------
using namespace std;
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define FIRSTNAME_INPUT_FILE ("E:\\firstname.txt")
#define LASTNAME_INPUT_FILE ("E:\\lastname.txt")
#define OUTPUT_FILE ("E:\\fullname.txt")
//StringSplit.h,cpp ***************************************
typedef struct _TStringSplit
{
string buff;
char delim;
int numTokens;
char ** tokens;
} * TStringSplit;
#define MAX_TOKEN_LENGTH (255)
//Trims white spaces
char *leftTrim(char *s)
{
while(isspace(*s)) s++;
return s;
}
char *rightTrim(char *s)
{
char* back = s + strlen(s);
while(isspace(*--back));
*(back+1) = '\0';
return s;
}
char *Trim(char *s)
{
return rightTrim(leftTrim(s));
}
//side effect: the tokens in the StringSplit structure must be freed
int _split ( TStringSplit strSplitRec)
{
int nLength = strSplitRec->buff.length()+1;
char * strbuff = (char*)malloc(nLength);
strcpy(strbuff,strSplitRec->buff.c_str());
cout << "Parsing >" << strbuff << "<" << endl;
strSplitRec->numTokens=0;
//first pass gets # of tokens
for (int iLoop=0; iLoop<nLength; iLoop++)
{
if (strbuff[iLoop]==strSplitRec->delim)
{
strSplitRec->numTokens++;
}
}
strSplitRec->numTokens++;
//sets up the array of tokens
strSplitRec->tokens = (char**) (malloc(sizeof(char*)*strSplitRec->numTokens));
for (int iLoop=0; iLoop<strSplitRec->numTokens; iLoop++)
{
strSplitRec->tokens[iLoop] = (char *) malloc(MAX_TOKEN_LENGTH);
memset(strSplitRec->tokens[iLoop],0,MAX_TOKEN_LENGTH);
}
char * inbuffPtr=strbuff;
for (int iLoop=0; iLoop<strSplitRec->numTokens; iLoop++)
{
if (iLoop>0) { inbuffPtr=NULL; }
strncpy(strSplitRec->tokens[iLoop],Trim(strtok(inbuffPtr,&strSplitRec->delim)),MAX_TOKEN_LENGTH);
}
free(strbuff);
}
//frees the tokens
KillTokens(TStringSplit strSplitRec)
{
for (int iLoop=0; iLoop<strSplitRec->numTokens; iLoop++)
{
if (strSplitRec->tokens[iLoop] != NULL) { free(strSplitRec->tokens[iLoop]); }
}
if ((strSplitRec->tokens)!=NULL) { free(strSplitRec->tokens); }
}
//strSplit.cpp *****************************
Go()
{
//reads the entire file of first names into a single buffer
ifstream infileFirstname;
infileFirstname.open( FIRSTNAME_INPUT_FILE);
string firstNameBuff = " ";
string firstNameInbuff;
while (!infileFirstname.eof())
{
infileFirstname >> firstNameInbuff;
if (infileFirstname.eof()) { break; }
firstNameBuff = firstNameBuff + firstNameInbuff;
}
cout << "firstNameBuff:>" << firstNameBuff << "<" << endl;
infileFirstname.close();
//parses the first names
struct _TStringSplit firstNameTokens;
firstNameTokens.buff = firstNameBuff;
firstNameTokens.delim = ',';
_split(&firstNameTokens);
for (int iLoop=0; iLoop<firstNameTokens.numTokens; iLoop++)
{
cout << ">" << firstNameTokens.tokens[iLoop] <<"<" << endl;
}
//reads the entire file of last names into a single buffer
ifstream infileLastname;
infileLastname.open(LASTNAME_INPUT_FILE);
string lastNameBuff = " ";
string lastNameInbuff;
while (!infileLastname.eof())
{
infileLastname >> lastNameInbuff;
if (infileLastname.eof()) { break; }
lastNameBuff = lastNameBuff + lastNameInbuff;
}
cout << "lastNameBuff:>" << lastNameBuff << "<" << endl;
infileLastname.close();
//parses the last names
struct _TStringSplit lastNameTokens;
lastNameTokens.buff = lastNameBuff;
lastNameTokens.delim = ',';
_split(&lastNameTokens);
for (int iLoop=0; iLoop<lastNameTokens.numTokens; iLoop++)
{
cout << ">" << lastNameTokens.tokens[iLoop] <<"<" << endl;
}
//finally writes the output
int N=-1;
if (lastNameTokens.numTokens==firstNameTokens.numTokens)
{
N = lastNameTokens.numTokens=firstNameTokens.numTokens;
}
else
{
cout << " Um, different # of first and last names in the file(s) " << endl;
N = (lastNameTokens.numTokens > firstNameTokens.numTokens) ? firstNameTokens.numTokens : lastNameTokens.numTokens;
}
ofstream outfile;
outfile.open(OUTPUT_FILE);
for (int iLoop=0; iLoop<N; iLoop++)
{
outfile << lastNameTokens.tokens[iLoop] << "," << firstNameTokens.tokens[iLoop] << "\n";
}
outfile.close();
KillTokens(&lastNameTokens);
KillTokens(&firstNameTokens);
}
int main()
{
Go();
}
Amarya W.
I haven't learned anything about what the code says. I'm a first-year student in CS is there a simpler code to get the first name last names on new lines?02/27/21
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
source code uploaded to RESROUCES section whose description is this link02/27/21