1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
#include <iostream>
#include <string>
#include <fstream> // opens file
#include <locale>
using namespace std;
ofstream fout("out.txt"); // Used to output invoice details
// function prototypes
string fixName(string n);
string fixSSN(string ss);
string fixPhone(string ph);
string fixAddress(string addr);
string fixCity(string cty);
string fixState(string st);
string extractField(string &s, char delimiter);
string fixZip(string zip);
void buildLine(string &s, string field);
int main()
{
ifstream inputFile;
// Variables needed to run program
string name;
string social;
string phone;
string address;
string city;
string state;
string line;
string zipcode;
inputFile.open("badnames.txt");
if (inputFile.fail())
cout << "Unable to open file! " << endl;
while (getline(inputFile, line))
{
name = extractField(line, '#');
name = fixName(name);
// print name into string
social = extractField(line, '#');
social = fixSSN(social);
// print social into string
phone = extractField(line, '#');
phone = fixPhone(phone);
// print phone number into string
address = extractField(line, '#');
address = fixAddress(address);
// print address into string
city = extractField(line, '#');
city = fixCity(city);
// print city into string
state = extractField(line, '#');
state = fixState(state);
// print state into string
zipcode = extractField(line, '#');
zipcode = fixZip(zipcode);
// print zipcode into string
// Displays functions
cout << fixName(name) << fixSSN(social) << fixPhone(phone) << fixAddress(address) << fixCity(city) << fixState(state) << fixZip(zipcode) << endl;
}
cout << "Data write complete." << endl;
cout << endl;
return 0;
}
string fixName(string n) // takes in the name and returns it in the right format (Lastname, Firstname)
{
string name;
name = n;
n.insert(-1, 0);
return n;
}
string fixSSN(string ss) // Fixes SSN (dashes included)
{
string sn;
sn = ss;
ss.insert(5, "-");
ss.insert(3, "-");
return sn;
}
string fixPhone(string ph) // Fixes phone number (dashes included)
{
string pho;
pho = ph;
ph.insert(3, "-");
ph.insert(6, "-");
return pho;
}
string fixAddress(string addr) // Fixes Address (adds in spaces)
{
string add;
add = addr;
addr.insert(3, " ");
addr.insert(6, " ");
addr.insert(11, " ");
return add;
}
string fixCity(string cty) // Fixes city
{
string cy;
cy = cty;
cty.insert(4, ",");
return cy;
}
string fixState(string st) // Fixes state
{
string sta;
sta = st;
st.insert(2, ",");
return sta;
}
string extractField(string &s, char delimiter) // Extracts a field eg name from the input and string and then deletes it and the pound that follows it
{
int loc;
string str;
loc = s.find(delimiter);
str = s.substr(0, loc);
s.erase(0, loc + 1);
return str;
}
string fixZip(string zip) // Fixes zipcode
{
string zp;
zp = zip;
zip.insert(6, 0);
return zp;
}
void buildLine(string &s, string field) // Concatenates field and a comma to string s
{
}
| |