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;           
       }

Recommended Answers

All 3 Replies

string parseInputString (string)
       {
       string oneline;   
       string last, first, result;   
       oneline = last + ", " + first + ".";
       return oneline;           
       }

This function will always return ", ." because you never put anything in oneline, last, first or result. Also, whatever variable you're feeding to this function has no name, so you can't use it even if you wanted to.

Thanks for helping me out! Okay, so I did these changes. But I still get the same result. Am I still just way off? I feel so dumb right now! =C

//******************************************************************************
//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 oneline)
       {
       string newName;   
       string last, first, result;   
       newName = last + ", " + first + ".";
       return newName;           
       }

Your function parseInputString build a string out of last and first.

last and first are empty in that function. You create them on line 88 and never put any values in them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.