This assignment is to give you practice using enums, string variables, and strin
ID: 3572445 • Letter: T
Question
This assignment is to give you practice using enums, string variables, and string functions. In order to get full credit for the program you must use these three topics. You are to write a program that will read a series of names of people from a data file that has been created with extra blank spaces and reformat the names into a standardized format. The datafile is mp5names.txt. It is arranged with the information for one person on each line. The first character on the line will be either ‘M’ or ‘F’ indicating the gender of the person, and the second character will be either ‘M’, ‘S’, or ‘D’ indicating married, single or divorced. You may assume that there will be no bad data here. The remainder of the line is the person’s name in the form: Last_name, First_name Middle_Initial. Note that there will always be a comma immediately after the last name and, if the person has middle initial, it will always be followed by a period. However, there could be any number of blank spaces in between each part of the name and some people do not have middle initial. Your task is to clean up the name and print it out in the standard format, e.g. Mr. Bill T. Jones with the appropriate title and exactly one space between each part of the name. You are REQUIRED to use functions, enums, string variables and string functions in the solution to this problem. Define an enum for the Marital Status of the person (with the values SINGLE, MARRIED and DIVORCED) and write a function that reads in the character from the file and returns the appropriate enum value. Read the name from the file into a string variable and write another function that uses the functions of the string class (e.g. find(), substr() etc.) to clean up the name and create a new string variable containing the name in the new format. Finally, add the appropriate title to the name and print it out. All males will have the title “Mr.” – married females will have “Mrs.” and single or divorced females will have “Ms.” Continue doing this until the end of file is reached. Your output must include the original name as read from the file followed by the new name in the standardized format. For formatting purposes you may assume that no name will have more than 30 characters. This is just a SUGGESTED method and for each step you may want to include output for debugging purposes to insure that your program is working correctly. Feel free to design your own modules as you like. A sample output from the first few lines of the data file follows: Original name Standardized name Bach, Johann S. Mr. Johann S. Bach Curie, Marie A. Mrs. Marie A. Curie Parker, Alice M. Ms. Alice M. Parke
Explanation / Answer
//headers
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <iomanip>
using namespace std;
typedef enum{SINGLE,MARRIED,DIVORCED} MARITAL_STATUS;
//function to return marital status of person
MARITAL_STATUS get_marital_status(char status)
{
MARITAL_STATUS ms;
switch(status)
{
case 'S':
ms=SINGLE;
break;
case 'M':
ms=MARRIED;
break;
case 'D':
ms=DIVORCED;
break;
}
return ms;
}
//function to return title for a person based on its gender and marital status
string get_title(char gender,MARITAL_STATUS status)
{
string title="";
if(gender=='M')
{
title="Mr.";
}
else if(gender=='F')
{
if(status==SINGLE || status==DIVORCED)
{
title="Ms.";
}
else if(status==MARRIED)
{
title="Mrs.";
}
}
return title;
}
//function to get name of a person in full form
string get_full_name(string name)
{
string standard_name="";
string last_name, first_name, middle_name, full_name;
//first remove leading spaces if any
string pattern="ABCDEFHIJKLMNOPQRSTUVWXYZ";
string whitespace=" ";
int start;
if(name.at(0)==' ')
{
start=name.find_first_of(pattern);
name=name.substr(start,name.length());
}
//extract last name
int comma_loc=name.find(',');
last_name=name.substr(0,comma_loc+1);
last_name=last_name.substr(0,last_name.length()-1);
//remaining name after extracting last name
string temp_name=name.substr(comma_loc+1,name.length());
//remove leading whitespace from remaining name if any
if(temp_name.at(0)==' ')
{
start=temp_name.find_first_of(pattern);
temp_name=temp_name.substr(start,temp_name.length());
}
//extract first name
int whitespace_loc=temp_name.find_first_of(whitespace);
first_name=temp_name.substr(0,whitespace_loc+1);
//remove trailing spaces from first name if any
if(first_name.at(first_name.length()-1)==' ')
{
int end=first_name.find_first_of(' ');
first_name=first_name.substr(0,end);
}
//remaining name after extracting first name. it will be middle name
temp_name=temp_name.substr(whitespace_loc+1,temp_name.length());
middle_name=temp_name;
//removing leading space from middle name if any provided middle name exists
if(middle_name.length()!=0 && middle_name.at(0)==' ')
{
start=middle_name.find_first_of(pattern);
middle_name=middle_name.substr(start,temp_name.length());
}
//once all name parts are extracted, concatenate them
if(middle_name.length()!=0) //if middle name exists
{
standard_name=first_name+" "+middle_name+" "+last_name;
}
else
{
standard_name=first_name+" "+last_name;
}
return standard_name;
}
//function to get name in standard form along with title
string get_standard_name(string title,string name)
{
return (title+" "+name);
}
//function to process the file containing names data
void process_file(string filename)
{
ifstream infile(filename.c_str());
//check if all ok.
if(!infile)
{
cerr<<"Error reading file..."<<endl;
cerr<<"Aborting..."<<endl;
exit(1);
}
//if all ok, process file
string line;
char gender,status;
MARITAL_STATUS marital_status;
string title,name,full_name;
cout<<"Original name"<<setw(30)<<"Standardized name"<<endl;
//process line read
while(getline(infile,line))
{
gender=line.at(0);
status=line.at(1);
marital_status=get_marital_status(status);
title=get_title(gender,marital_status);
name=line.substr(2,line.length());
full_name=get_standard_name(title,get_full_name(name));
string temp_name=name.substr(name.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),name.length()+1);
cout<<temp_name<<setw(30)<<full_name<<endl;
}
}
int main() {
string filename; //name of file to process
string root_path="./src/"; //location where file is stored. you can change this as per your location
cout<<"Enter filename to process:";
cin>>filename;
string file_path=root_path+filename;
cout<<"Processing file at "<<file_path<<"..."<<endl;
process_file(file_path);
cout<<"Processing complete!"<<endl;
return 0;
}
-----------------------------------------------
Input file: names.txt
-----------------------------------------------
MS Bach, Johann S.
FMCurie, Marie A.
FS Parker, Alice M.
FD Meir,Golda T.
MDRobeson, Paul
MM Ashe, Arthur
FM Tubman, Harriet A.
MSBraille,Louis
-----------------------------------------------------
Output:
-----------------------------------------------------
Enter filename to process:names.txt
Processing file at ./src/names.txt...
Original name Standardized name
Bach, Johann S. Mr. Johann S. Bach
Curie, Marie A. Mrs. Marie A. Curie
Parker, Alice M. Ms. Alice M. Parker
Meir,Golda T. Ms. Golda T. Meir
Robeson, Paul Mr. Paul Robeson
Ashe, Arthur Mr. Arthur Ashe
Tubman, Harriet A. Mrs. Harriet A. Tubman
Braille,Louis Mr. Louis Braille
Processing complete!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.