I am doing a program involving an unsorted list of names that I have to turn form Firstname Lastname into Lastname, Firstname. So I wrote the main program, and my instructor told me that under my getList function, to retrieve the data just do data >> first and data >> last within my loop, but later on to go back and switch it to getline. So, I'm not really sure how to do that properly. I don't think I'm declaring my string properly, because after I commented out the data << first and data << last, all I get on my out file is a comma and a period, so it's definitely not reading my data. Can someone point me in the right direction? I have to use getline. Its part of the assignment. Thanks!!!
//******************************************************************************
//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...
string parse2 (string first, string last);
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 first, last, result;
string oneline;
//data >> first;
while (data)
{
//data >> last;
result = parse2(first,last);
mylist.Insert(result);
getline (data, oneline);
// data >> first;
}
}
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 parse2 (string first, string last)
{
string personName;
personName = last + ", " + first + ".";
return personName;
}
//www.cplusplus.com/doc/tutorial/classes/