Advertisement
lwytop

Wonyoung Lee mp6

Jul 21st, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. // ConsoleApplication4.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <string>
  7. #include <fstream>
  8. #include <iomanip>
  9.  
  10. using namespace std;
  11.  
  12. enum gender {       //use enum to set male and female value.
  13.     male = 1,
  14.     female
  15. };
  16.  
  17. enum marriage {     //use enum to set marriage value M, S, D.
  18.     married = 1,
  19.     single,
  20.     divorced
  21. };
  22.  
  23. string Erase_Blank(string data) {           //use this fuction to erase blank on the string.
  24.  
  25.     string fixedData = "";
  26.  
  27.     for (size_t i = 0; i < data.length(); i++)
  28.     {
  29.         if (data[i] != ' ') {               //if character is not blank put in the fixedData.
  30.             fixedData += data[i];      
  31.         }
  32.     }
  33.     return fixedData;                       //return that.
  34. }
  35.  
  36. int main()
  37. {
  38.     string data = "";                       //data get on the file.
  39.     string fixedData = "";                  //data after erase blank.
  40.  
  41.     string firstName = "";                 
  42.     string lastName = "";
  43.     string middleName = "";
  44.     string prefix = "";                     //get data to set male/female and married/ single/ divorced to make prefix like Mr. Ms. Mrs.
  45.  
  46.     string originalName = "";               //last, first, middle name data.
  47.  
  48.  
  49.     int index = 0;                          //find point of divide last name and first name.
  50.     int endPoint = 0;                       // the point of this string's end.
  51.  
  52.     gender g = male;
  53.     marriage m = married;
  54.  
  55.  
  56.     ifstream inputFile;
  57.     inputFile.open("c:\\temp\\mp6.txt");
  58.  
  59.     cout.setf(ios::left);
  60.     cout << setw(25) << "Original Name." << "Standardized Name." << endl;  
  61.     cout << "-------------------------------------------" << endl;
  62.  
  63.     while (!inputFile.eof()) {
  64.  
  65.         getline(inputFile, data);                   //get line of data.
  66.         fixedData = Erase_Blank(data);              //and use Erase_Blank fuction to erase blank in the string
  67.  
  68.         endPoint = fixedData.length();             
  69.        
  70.  
  71.         if (fixedData[0] == 'M') {
  72.             g = male;                   //save enum value (male = 1)
  73.         }
  74.         else if (fixedData[0] == 'F') {
  75.             g = female;                 //save enum value (female = 2)
  76.         }
  77.        
  78.        
  79.  
  80.         if (fixedData[1] == 'S') {              //similar than above. single = 2
  81.             m = single;
  82.         }
  83.         else if (fixedData[1] == 'M') {         //married = 1
  84.             m = married;
  85.         }
  86.         else if (fixedData[1] == 'D') {         //divorced = 3
  87.             m = divorced;
  88.         }
  89.  
  90.         switch (g) {
  91.         case male:
  92.             prefix = "Mr.";                     //all male use Mr. , so use gender to define prefix.
  93.             break;
  94.         case female:
  95.             if (m == single || m == divorced) {         //single or divorced woman's prefix is Ms.
  96.                 prefix = "Ms.";
  97.        
  98.             }
  99.             else if (m == married) {                    //married woman need Mrs.
  100.                 prefix = "Mrs.";
  101.        
  102.                
  103.             }
  104.             break;
  105.         }
  106.  
  107.  
  108.         index = fixedData.find(',');            //find ',' to find endpoint of last name.
  109.  
  110.         lastName = fixedData.substr(2, index - 1);
  111.  
  112.         if (fixedData[endPoint-1] == '.') {                     //find '.' to find about middle name's existence.
  113.             middleName = fixedData.substr(endPoint - 2, 2);     //if it exists find middle name.
  114.            
  115.             firstName = fixedData.substr(index + 1, endPoint - 3 - index);      //and find first name.
  116.         }
  117.         else {
  118.             firstName = fixedData.substr(index + 1, endPoint - 1);              //if not, just find firstname.
  119.         }
  120.  
  121.         cout.setf(ios::left);       //set left side to look good.
  122.  
  123.         originalName = lastName + " " + firstName + " " + middleName;       //before use setw it make help easily put original name category.
  124.  
  125.         cout << setw(25) << originalName <<
  126.             prefix << " " << firstName << " " << middleName << " " << lastName << endl;
  127.  
  128.  
  129.  
  130.     }
  131.  
  132.  
  133.  
  134.     system("pause");
  135.     return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement