I am getting a abort() error

I am working on a project for a class and my code is getting the error. If somebody could help fix my code that would be awesome.

The thing we are trying to do is take a file and update whats in it. Example file will have
john smith#16598007@8148337965#3rd & state st#erie#pa#16506
then format it to
Smith, John, 165-98-0076, 814-833-7965, 3rd & State St, Erie, PA 16506
and output that to a different file.

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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>

using namespace std;

//function prototypes
string extractField(string &input);
string fixName(string &input);
string fixSSN(string &input);
string fixPhone(string &input);
string fixAddress(string &input);
string fixCity(string &input);
string fixState(string &input);
string getZip(string &input);
void buildLine(string name, string SSN, string phone, string addr, string city, string state, string zip, ofstream &fout);

int main()
{

	string input, name, SSN, phone, addr, city, state, zip;

	ifstream fin;
	ofstream fout;


	fin.open("badnames.txt");

	if (fin.fail())
	{
		cout << "File open failed" << endl;
	}
	else
	{
		fout.open("output.txt");
		if (fout.fail())
		{
			cout << "File open failed" << endl;
		}
		else
		{
			while (getline(fin,input))
			{

				name = fixName(input);
				SSN = fixSSN(input);
				phone = fixPhone(input);
				addr = fixAddress(input);
				city = fixCity(input);
				state = fixState(input);
				zip = getZip(input);
				buildLine(name, SSN, phone, addr, city, state, zip, fout);

			}
			fout.close();
		}

		fin.close();
	}

	cout << "Complete" << endl;

	return 0;
}

string extractField(string &input)
{
	int num;
	string s;
	num = input.find('#', 0);
	s = input.substr(0, num);
	input.erase(0, num);
	return s;
}

string fixName(string &input)
{
	char temp;
	int num;
	string first, last, name;
	name = extractField(input);

	num = name.find(' ', 0);
	first = name.substr(0, num);
	name.erase(0, num);
	last = name;

	temp = first.front();
	temp = toupper(temp);
	first.erase(0, 1);
	first.insert(0, 1, temp);

	temp = last.front();
	temp = toupper(temp);
	first.erase(0, 1);
	last.insert(0, 1, temp);

	last.append(1, ',');
	first.append(1, ',');

	name = last + first;
	return name;

}

string fixSSN(string &input)
{
	string SSN;
	SSN = extractField(input);

	SSN.insert(4, 1, '-');
	SSN.insert(7, 1, '-');

	SSN.append(1, ',');

	return SSN;
}

string fixPhone(string &input)
{
	string phone;
	phone = extractField(input);

	phone.insert(4, 1, '-');
	phone.insert(8, 1, '-');

	phone.append(1, ',');

	return phone;
}

string fixAddress(string &input)
{
	string addr;
	addr = extractField(input);

	addr.append(1, ',');

	return addr;
}

string fixCity(string &input)
{
	string city;
	int temp;
	city = extractField(input);

	temp = city.front();
	temp = toupper(temp);
	city.erase(0, 1);
	city.insert(0, 1, temp);

	city.append(1, ',');

	return city;
}

string fixState(string &input)
{
	string state;
	int temp;
	state = extractField(input);

	temp = state.front();
	temp = toupper(temp);
	state.erase(0, 1);
	state.insert(0, 1, temp);

	temp = state.back();
	temp = toupper(temp);
	state.erase(1, 1);
	state.insert(1, 1, temp);

	return state;
}

string getZip(string &input)
{
	string zip;
	zip = input;
	return zip;
}

void buildLine(string name, string SSN, string phone, string addr, string city, string state, string zip, ofstream &fout)
{
	fout << name << SSN << phone << addr << city << state << zip << endl;
}
You read a line from the file via getline and store it in string 'input'. First this string, input, is passed to fixName() who, in turn, passes input on to extractField(). Now what does extractField() do? It finds the first occurrence of '#' and splits up the string, input, from the start to just before the # appears (input.substr(0,num)). OK, so far so good (I hope) but then what does extractField() do next? Well it goes ahead and erases the part of the input string that holds the name (input.erase(0, num)) and returns an empty string. I stopped reading after this ...

Rather than pass the whole string input around to various functions you should try and break up this string into its constituent parts as soon as you can so that you have tighter control over the data and can work with smaller individual segments. I shall try and come back later with a suggestion on these lines.
The problem is in this function:
1
2
3
4
5
6
7
8
9
10
11
12
string fixSSN(string &input)
{
  string SSN;
  SSN = extractField(input);

  SSN.insert(4, 1, '-');
  SSN.insert(7, 1, '-');

  SSN.append(1, ',');

  return SSN;
}

The function extractField returned an empty string and then SSN.insert will crash.
How do you recommend fixing this?
Line 5: Check if SSN is empty and return the empty string.
Is there a reason why you make it so complicated? You could split the input line with just one function and check if the number of tokens is right. Quick demo - not properly tested:
1
2
3
4
5
6
7
8
9
10
11
void SplitString(const string& src, char sep, StringVector& output)
{
  size_t strpos = 0, endpos = src.find(sep); 
  while (endpos < src.length())
  {
    output.push_back(src.substr(strpos, endpos - strpos));
    strpos = endpos + 1;
    endpos = src.find(sep, strpos);
  }
  output.push_back(src.substr(strpos));
}
There's a critical typo in the OP: SSN in the sample input file has 8 digits and SSN in the sample output file has 9 digits. Until I realized this it caused me no end of trouble throwing various exceptions. Anyways, full solution is given below, please read, research and only then return if something is still unclear:

edit: requires C++11 compiler

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
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<cctype>
#include<locale>
#include<vector>
#include<algorithm>
using namespace std;
struct Person
{
    string m_first_name;
    string m_last_name;
    string m_SSN;
    string m_phone;
    string m_address;
    string m_city;
    string m_state;
    string m_ZIP;
};
std::ofstream& operator<<(std::ofstream& fout, const Person& p)
{
    fout<<p.m_last_name<<", "<<p.m_first_name<<", "<<p.m_SSN<<", "<<p.m_phone<<", "<<p.m_address<<", "<<p.m_city
    <<", "<<p.m_state << ", "<<p.m_ZIP<<"\n";
    return fout;
}
int main()
{
    string input;
    ifstream fin;
    ofstream fout("F:\\test1.txt");
    vector<Person> v;

    fin.open("F:\\test.txt");
    if(fin.is_open())
    {
        while(getline(fin, input))
        {
        Person p;
        replace_if(input.begin(), input.end(), [](char s){return ((s == '@') || (s == '#')); }, ';');
        stringstream stream(input);
        stream>> p.m_first_name;
        static const auto delimiter = ';';
        getline(stream, p.m_last_name, delimiter)&&
        getline(stream, p.m_SSN, delimiter)&&
        getline(stream, p.m_phone, delimiter)&&
        getline(stream, p.m_address, delimiter)&&
        getline(stream, p.m_city, delimiter)&&
        getline(stream, p.m_state, delimiter)&&
        stream>>p.m_ZIP;

        p.m_first_name = toupper(p.m_first_name[0], locale()) + p.m_first_name.substr(1);

        p.m_last_name.erase(p.m_last_name.begin()+0);
        p.m_last_name = toupper(p.m_last_name[0], locale()) + p.m_last_name.substr(1);

        p.m_SSN.insert(3,"-");
        p.m_SSN.insert(6, "-");

        p.m_phone.insert(3, "-");
        p.m_phone.insert(7, "-");

        stringstream stream_address(p.m_address);
        string word;
        vector<string> v1;

        while(stream_address>>word)
        {
            v1.push_back(word);
        }
        p.m_address.clear();
        for(auto& itr1 : v1)
        {
            itr1 = toupper(itr1[0], locale()) + itr1.substr(1);
            p.m_address += itr1 + " ";
        }
        p.m_address.pop_back();

        p.m_city = toupper(p.m_city[0], locale()) + p.m_city.substr(1);

        for (unsigned int i = 0; i < p.m_state.size(); i++)
        {
            p.m_state[i] = toupper(p.m_state[i], locale());
        }

        v.emplace_back(p);

        }
    }

    for(auto& itr: v)
    {
        fout<<itr;
    }
}


Sample Input File
john smith#165980076@8148337965#3rd & state st#erie#pa#16506

Resultant Output File
Smith, John, 165-98-0076, 814-833-7965, 3rd & State St, Erie, PA, 16506

Last edited on
Topic archived. No new replies allowed.