I need to take an input file that contains name listed like:
John Henry
Mary Baker
and create an output that looks like:
Baker, Mary.
Henry, John.
Right now my output is just
,.
,.
,.
,.
,.
,.
I can't use any global accesses, or a character processing alogorithm. I need to parse the line into a last name and a first name and then concatenate them so they look like the above example. This is my attempt, but I am really lost in my stringParseInput (string) algorithm. I am only getting
,.
,.
,.
,.
,.
,.
,.
Thanks for the advice!
=//******************************************************************************
//MAIN PROGRAM
//******************************************************************************
/* Program: Name Lists
* Description: To create an unsorted list of strings, parse the strings, and
* use classes to inplement a list of names.
*/
# include <iostream>
# include <fstream>
# include <string>
# include <cctype>
using namespace std;
#include "list.cpp"
void getList (ifstream&, List&);
// uses EOF loop to extract list of names from data file...
void putList (ofstream&, List);
// uses call-by-value because iterators modify the class...
string parseInputString (string);
// will be called from GetList...
int main ()
{
ifstream data;
ofstream out;
List mylist;
data.open ("name_list.txt"); //file for input...
if (!data)
{
cout << "ERROR!!! FAILURE TO OPEN name_list.text" << endl;
system ("pause");
return 1;
}
out.open ("out.txt"); //file for output...
if (!out)
{
cout << "ERROR!!! FAILURE TO OPEN out.text" << endl;
system ("pause");
return 1;
}
getList (data, mylist);
putList (out, mylist);
data.close ();
out.close ();
system ("pause");
return 0;
} // main
void getList (ifstream& data, List& mylist)
{
string oneline;
string first, last, result;
while (data)
{
getline (data, oneline);
result = parseInputString(oneline);
mylist.Insert(result);
}
}
void putList (ofstream& outfile, List mylist)
{
string oneline;
//use iterators
mylist.ResetList ();
outfile << "Names: " << endl << endl;
while (mylist.HasNext ())
{
oneline = mylist.GetNextItem ();
outfile << oneline << endl;
}
}
string parseInputString (string)
{
string oneline;
string last, first, result;
oneline = last + ", " + first + ".";
return oneline;
}