Need help on following program using C++ langugae. Write a function to convert a
ID: 3687375 • Letter: N
Question
Need help on following program using C++ langugae.
Write a function to convert a string containing lastName, firstName middleName to firstName middleName lastName. For example, if the string contains: "Miller, Jason Brian", then the altered string should contain "Jason Brian Miller." A data file with names has been provided for testing. Write a driver that reads each line in as a string, uses your function to convert the name and then displays the converted name.
write source code which should be commented as follows:
Analysis at the top of the file
Function analysis, preconditions, and postconditions after each function prototype
Major tasks identified as comments in main function
Comments in functions as needed
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct nameType
{
string fname;
string mname;
string lname;
nameType()
{
reset();
}
void reset()
{
fname = "";
mname = "";
lname = "";
}
void print(ostream& os)
{
os << fname;
if (mname.length() > 0) os << " " << mname;
if (lname.length() > 0) os << " " << lname;
cout << endl;
}
};
void split_line(const string& strLine, nameType& dest)
{
size_t pos_b = 0, pos_c = 0;
dest.reset();
pos_b = strLine.find(' ');
if (pos_b != string::npos)
{
dest.fname = strLine.substr(0, pos_b);
pos_c = strLine.find(' ', pos_b + 1);
if (pos_c != string::npos)
{
dest.mname = strLine.substr(pos_b + 1, pos_c - pos_b - 1);
dest.lname = strLine.substr(pos_c + 1);
}
else
{
dest.mname = "";
dest.lname = strLine.substr(pos_b + 1);
}
}
else
{
dest.fname = strLine;
dest.mname = "";
dest.lname = "";
}
}
int main()
{
string name;
size_t pos_a, pos_b, pos_c;
nameType nt;
while(true)
{
nt.reset();
cout << "Enter name which contains completely> ";
getline(cin, name);
split_line(name, nt);
nt.print(cout);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.