C++

closed account (2E1qDjzh)

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 mp6names.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. Parker


//
// main.cpp
// Machine Problem 6
//
// Created by Babafemi Omole on 4/23/18.
// Copyright © 2018 Babafemi Omole. All rights reserved.
//

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

enum married{SINGLE, MARRRIED};
string ReadString(ifstream & fin){
string names;
getline(fin, names);
return names;
}


int main()
{
ifstream names("names.txt");
string line, last, first, MI;
long int a;
married person;
line = ReadString(names);
while(names)
{
MI = " ";
if (line[0] == 'M')
{
cout << "Mr. ";
}
else
{
if( line[1] == 'S' || line[1] == 'D')
{
person = SINGLE;
if(person == SINGLE)
{
cout << "Ms. ";
}
}

else
{
person = MARRRIED;
if(person == MARRRIED)
{
cout << "Mrs. ";
}
}
}
a = line.find(',');
last = line.substr(2, a - 2);
first = line.substr(a + 1);
a = 0;
while(last[a] == ' ')
{
a++;
}

last = last.substr(a);
a = 0;
while(first[a] == ' '){
a++;
}
first = first.substr(a);
a = 0;
while(first[a] != ' '){
a++;

}
first = first.substr(0, a);
a = line.find('.');
if(a != -1){
MI = line.substr(a -1 , 1);
}

cout << last << ' ';
cout << MI << '.';
cout << first << endl;
line = ReadString(names);

}

return 0;

}



Output:

Mr. Bach S.Johann
Mrs. Curie A.Marie
Ms. Parker M.Alice
Ms. Meir T.Golda
Mr. Robeson .Paul
Mr. Ashe .Arthur
Mrs. Tubman A.Harriet
Mr. Braille .Louis


how do I remove the space with no middle initial
Topic archived. No new replies allowed.