Confused

I've hit a roadblock and I'm not sure how to proceed, I'm writing a code that reads in a file of names and adresses and writes it out to a comma separated file, but I'm not sure how to proceed from here:
Needs to be reformatted to - Lastname, Firstname, Social Number, Phone Number, Address, City, State, Zipcode. Thanks for your help!

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
{

}


What the text file looks like;
john smith#165980076#8148337965#3rd & state st#erie#pa#16506
martin gardner#164905543#4403542700#5244 heisley rd#mentor#oh#44123
sarah vendetti#164879987#3302541122#29 millhaven st.#akron#oh#44532
judy golumbiewski#194409976#7163347654#6187 fillmore ave#rochester#ny#07654
terry merz#193976554#8002435799#19876 elm avenue#manchester#mo#63166
kenisha jones#198432265#9193610419#902 east taylor rd#apex#nc#27709
phil yee#176409976#6308602682#1976 wacker drive#chicago#il#60191
jean caron#167453321#9738874700#1429 delben street#whippany#nj#07981
sandy alicea#176419006#3054428202#12 la habana east#miami#fl#33134
rob williams#206337765#3342715450#9854 fremont blvd#montgomery#al#36109
jancy hilfiger#201876987#8148817654#3012 state st#pittsburgh#pa#15011
98: n.insert(-1, 0);
You can't insert anything with an negative index.
To swap firstname and lastname it's better to use n.find(' ') or a stringstream.
Topic archived. No new replies allowed.