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>
usingnamespace 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;
}
No, I haven't learned how to use a debugger. What do you mean? Like after you complie and it tells you all the things that you screwed up? If so, then yes and no.
Oh! Duh! Line 85 in the (), it shouldn't say (string). It should say (string oneline), right? Ok, so let me try that, and see what happens. Thanks very very much!!!
Ok, so I changed it to this, but still no output... bummer. Sorry for being such a dummy...
I changed the string name to newName in the string parseInputString because it said it shadowed a parameter when it was listed as oneline... It compiles after I did the name change and gives no warnings to debug, by the way, so I don't know... Obviously this isn't right...
//******************************************************************************
//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>
usingnamespace 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 oneline)
{
string newName;
string last, first, result;
newName = last + ", " + first + ".";
return newName;
}
No. A debugger is a tool that runs alongside your programme. You can stop your programme at any point and interrogate the value of variables at that point.
For example, you could have had the debugger stop before and after you called the parseInputString function (i.e. on the line result = parseInputString(oneline); ) and check the values of all the variables, and you would then see that you were getting back from parseInputString something you did not expect - thus, you would know your mistake was in there somewhere.
Spend a few hours looking up debuggers and learning how to use them; the ability to pause your code anywhere and check the values of everything at that point will save you so much time.
It should say (string oneline), right? Ok, so let me try that, and see what happens. Thanks very very much!!!
So what exactly does that function do with the variable named oneline? Builds something out of last and first. Where in that function do last and first get given values?
Yes, I just emailed my instructor and he said the same thing. Actually, this is his reply:
"First, restructure the loop with the getline before and after the loop.
In you parse, you will be using the string functions from Chapter 3 to extract first and last name.
You will need some integer indexes. indexfirst,indexlast.
You will search the oneline string using the find operation, then do a substr starting at first to extract the first name.
You will also need the cctype and the .at function."
So... between what he said and you said, I can see more or less where I epically screwed up. I'm going to try and implement this and see how it works out. What is the .at function? i'll have to look that up. That's the thig with his labs. the book is really simplistic, and I'm such a beginner that little things like that screw me up. Who knew bout a .at function? Thanks for the advice on the debugger. I'll read up on that, definitely.