Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication4.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <iomanip>
- using namespace std;
- enum gender { //use enum to set male and female value.
- male = 1,
- female
- };
- enum marriage { //use enum to set marriage value M, S, D.
- married = 1,
- single,
- divorced
- };
- string Erase_Blank(string data) { //use this fuction to erase blank on the string.
- string fixedData = "";
- for (size_t i = 0; i < data.length(); i++)
- {
- if (data[i] != ' ') { //if character is not blank put in the fixedData.
- fixedData += data[i];
- }
- }
- return fixedData; //return that.
- }
- int main()
- {
- string data = ""; //data get on the file.
- string fixedData = ""; //data after erase blank.
- string firstName = "";
- string lastName = "";
- string middleName = "";
- string prefix = ""; //get data to set male/female and married/ single/ divorced to make prefix like Mr. Ms. Mrs.
- string originalName = ""; //last, first, middle name data.
- int index = 0; //find point of divide last name and first name.
- int endPoint = 0; // the point of this string's end.
- gender g = male;
- marriage m = married;
- ifstream inputFile;
- inputFile.open("c:\\temp\\mp6.txt");
- cout.setf(ios::left);
- cout << setw(25) << "Original Name." << "Standardized Name." << endl;
- cout << "-------------------------------------------" << endl;
- while (!inputFile.eof()) {
- getline(inputFile, data); //get line of data.
- fixedData = Erase_Blank(data); //and use Erase_Blank fuction to erase blank in the string
- endPoint = fixedData.length();
- if (fixedData[0] == 'M') {
- g = male; //save enum value (male = 1)
- }
- else if (fixedData[0] == 'F') {
- g = female; //save enum value (female = 2)
- }
- if (fixedData[1] == 'S') { //similar than above. single = 2
- m = single;
- }
- else if (fixedData[1] == 'M') { //married = 1
- m = married;
- }
- else if (fixedData[1] == 'D') { //divorced = 3
- m = divorced;
- }
- switch (g) {
- case male:
- prefix = "Mr."; //all male use Mr. , so use gender to define prefix.
- break;
- case female:
- if (m == single || m == divorced) { //single or divorced woman's prefix is Ms.
- prefix = "Ms.";
- }
- else if (m == married) { //married woman need Mrs.
- prefix = "Mrs.";
- }
- break;
- }
- index = fixedData.find(','); //find ',' to find endpoint of last name.
- lastName = fixedData.substr(2, index - 1);
- if (fixedData[endPoint-1] == '.') { //find '.' to find about middle name's existence.
- middleName = fixedData.substr(endPoint - 2, 2); //if it exists find middle name.
- firstName = fixedData.substr(index + 1, endPoint - 3 - index); //and find first name.
- }
- else {
- firstName = fixedData.substr(index + 1, endPoint - 1); //if not, just find firstname.
- }
- cout.setf(ios::left); //set left side to look good.
- originalName = lastName + " " + firstName + " " + middleName; //before use setw it make help easily put original name category.
- cout << setw(25) << originalName <<
- prefix << " " << firstName << " " << middleName << " " << lastName << endl;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement