Having trouble with code 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
189
190
191
192
193
#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);
				input.clear();

			}
			fout.close();
		}

		fin.close();
	}

	cout << "Complete" << endl;

	return 0;
}

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

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

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

	cout << first << last << endl;

	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;
}
Last edited on
Line 92,97,152,168,173: string::front() and string::back() were added in C++11. Are you sure you're using a C++11 compiler (or have C++11 enabled in your compiler)?
I am using the latest version of VS 2015 and I have used string::front() before and it worked.
I've retained the intended program logic unchanged, but made some modifications to the code.

I do not have the time or the inclination to comment the changes explaining the rationale, so I have retained most of the original code (verbatim) as commented out lines. Hopefully, you will be able to compare the original with the modified code and understand the reasons for the changes.

Tip: Take baby steps.
Write one function, test it, make sure it is working correctly before you move on to the next function.
For instance, first verify that extractField is indeed extracting the raw fields as expected.
Once that is working, move on to fixName, make sure that it is correct.
And so on.

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
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>

using namespace std;

//function prototypes
string extractField( string &input, char delimiter = '#' ); // added a second parameter with a default value
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, ostream &fout );

int main()
{

    string input, name, SSN, phone, addr, city, state, zip;
    input = "john smith#16598007@8148337965#3rd & state st#erie#pa#16506" ;
    std::cout << input << '\n' ;

    {

        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, cout );
        input.clear();

    }

    // cout << "Complete" << '\n';

    // return 0;
}

string extractField( string &input, char delimiter )
{
    // int num;
    // string s;
    // num = input.find( '#', 0 );
    const auto num = input.find( delimiter, 0 );
    // num -= 1;
    // s = input.substr( num, 0 );
    string s = input.substr( 0, num ) ;
    // s = input.substr(num, 0);
    input.erase( 0, num + 1 );
    return s;
}

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

    auto num = name.find( ' ', 0 );
    // num -= 1;
    string first = name.substr( 0, num );
    // name.erase( 0, num );
    name.erase( 0, num + 1 ) ;
    string last = name;

    // cout << first << last << '\n';

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

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

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

    return last + ", " + first + ", ";
}

string fixSSN( string &input )
{
    // string SSN;
    // SSN = extractField( input );
    string SSN = extractField( input, '@' );

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

    // SSN.append( 1, ',' );
    SSN += ", " ;

    return SSN;
}

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

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

    // phone.append( 1, ',' );

    return phone + ", " ;
}

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

    // addr.append( 1, ',' );

    return addr + ", ";
}

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

    /*
    temp = city.front();
    temp = toupper( temp );
    city.erase( 0, 1 );
    city.insert( 0, 1, temp );
    */
    city.front() = toupper( city.front() ) ;
    // city.append( 1, ',' );

    return city + ", " ;
}

string fixState( string &input )
{
    // string state;
    // int temp;
    string 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 );
    */

    for( char& c : state ) c = toupper( c ) ;

    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, ostream &fout )
{
    fout << name << SSN << phone << addr << city << state << zip << '\n';
}

http://rextester.com/CKPWE64146
Topic archived. No new replies allowed.