As one of your first programming assignments at Mega Industries, you have been t
ID: 3898446 • Letter: A
Question
As one of your first programming assignments at Mega Industries, you have been tasked with writing a program in C++ to read a file of names and addresses which was extracted from an obsolete system and write it out to a comma separated file. Many of the fields need to be edited. A typical line in the old file would be:
john smith#165980076#8148337965#3rd & state st#erie#pa#16506
it needs to be reformatted to:
Smith,John,165-98-0076,814-833-7965,3rd & State St,Erie,PA,16506
After consulting with a senior programmer, you have a list of required functions as follows:
string fixName (string n);
//takes in the name and returns it in the right format
string fixSSN (string ss); //Fixes SSN
string fixPhone(string ph); // Fixes phone number
string fixAddress(string addr); //Fixes address
string fixCity(string cty); // Fixes city
string fixState(string st); // Fixes state
string extractField(string &s);
//Extracts a field eg name from the input string and then deletes
//it and the octothorpe (#) that follows it. Notice string s is
//passed by reference There` is a copy of this function on the
// slides for lecture #20
void buildLine(string &s, string field);
// Concatenates field and a comma to string s
Please use the following as a way to input the file.
ifstream inputFile("badnames.txt");
inputFile.open( Location of file)
The text file is as follows:
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
Explanation / Answer
Below is your answer.Please do rate this answer positive, If i was able to help you. Let me know if you have any issues in comments
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
using namespace std;
//function prototypes
string extractField( string &input); // 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;
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)
{
char delimiter = '#';
const auto num = input.find( delimiter, 0 );
string s = input.substr( 0, num ) ;
input.erase( 0, num + 1 );
return s;
}
string fixName( string &input )
{
string name = extractField( input );
auto num = name.find( ' ', 0 );
string first = name.substr( 0, num );
name.erase( 0, num + 1 ) ;
string last = name;
first.front() = toupper( first.front() ) ;
last.front() = toupper( last.front() ) ;
return last + "," + first + "," ;
}
string fixSSN( string &input )
{
string SSN = extractField( input );
SSN.insert( 4, 1, '-' );
SSN.insert( 7, 1, '-' );
SSN += "," ;
return SSN;
}
string fixPhone( string &input )
{
// string phone;
string phone = extractField( input );
phone.insert( 4, 1, '-' );
phone.insert( 8, 1, '-' );
return phone + "," ;
}
string fixAddress( string &input )
{
string addr = extractField( input );
int len = addr.length();
for(int i = 1;i < len;i++) {
if(addr[i-1] == ' ') {
addr[i] = toupper(addr[i]);
}
}
return addr + "," ;
}
string fixCity( string &input )
{
string city = extractField( input );
city.front() = toupper( city.front() ) ;
return city + "," ;
}
string fixState( string &input )
{
string state = extractField( input );
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 << ' ';
}
Output
Smith,John,1659-80-076,8148-337-965,3rd & State St,Erie,PA,16506
Gardner,Martin,1649-05-543,4403-542-700,5244 Heisley Rd,Mentor,OH,44123
Vendetti,Sarah,1648-79-987,3302-541-122,29 Millhaven St.,Akron,OH,44532
Golumbiewski,Judy,1944-09-976,7163-347-654,6187 Fillmore Ave,Rochester,NY,07654
Merz,Terry,1939-76-554,8002-435-799,19876 Elm Avenue,Manchester,MO,63166
Jones,Kenisha,1984-32-265,9193-610-419,902 East Taylor Rd,Apex,NC,27709
Yee,Phil,1764-09-976,6308-602-682,1976 Wacker Drive,Chicago,IL,60191
Caron,Jean,1674-53-321,9738-874-700,1429 Delben Street,Whippany,NJ,07981
Alicea,Sandy,1764-19-006,3054-428-202,12 La Habana East,Miami,FL,33134
Williams,Rob,2063-37-765,3342-715-450,9854 Fremont Blvd,Montgomery,AL,36109
Hilfiger,Jancy,2018-76-987,8148-817-654,3012 State St,Pittsburgh,PA,15011
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.