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/

Recommended Answers

All 4 Replies

On line 64, you are attempting to parse the data without having any data yet. Put your getline statement first (before your parsing statement), then put a statement to split your string in two (into first and last), and then finally call parse2() .

Ooooh, ok. I'll try that. I was just going by where my instructor stuck the getline. He probably didn't notice that or mean for me to take him so literally. Haha. Makes sense.

Ok, this is what I have now, but my output is still just
,.
,.
,.
,.
,.
What the heck am I missing and where? Aaah! Thanks for helping me out though...

//******************************************************************************
//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 oneline;
            string first, last, result;
            while (data)            
              {                
                getline (data, oneline);
                result = parse2(first,last);  
                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 parse2 (string first, string last)
       {
       string personName;      
       personName = last + ", " + first + ".";
       return personName;           
       }

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/

You're passing "first" and "last" to your function parse2 without giving those variables any value or meaning. In your getlist function you must first do some work on the string "oneline" to split it into "first" and "last". Use something like string.find(0, ' '), which gives you the location of the space, then use string.substr to generate two substrings, one before the space, and one after. Put those in your first and last, and you're good to go.

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.