Getline??? I need a bit of help!

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!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//******************************************************************************
//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/
Line 66. Surely you want to grab the data from the file with getline before you parse the results into the output file. So do this rather:
1
2
3
4
5
6
while (data)            
{                
    getline (data, oneline);
    result = parse2(first,last);  
    mylist.Insert(result);
}


Line 77. What does ResetList() do?
mylist.ResetList ();
Last edited on
Hey! Thanks for the reply. I actually just tried that but my output is still just
, .
, .
, .
, .
, .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void getList (ifstream& data, List& mylist)
      {
            
            string oneline;
            string first, last, result;
            while (data)            
              {                
                getline (data, oneline);
                result = parse2(first,last);  
                mylist.Insert(result);
                                          
              }
      }
      

That's what I have now. I'm not 100% sure what reset list does! Haha! It was something in the book that we "had to include", but I can't find an example in the book of the getline that would read from a data file. I see only things like getline (cin, string). I have a feeling I'm missing something really minor and silly... Ah! frustration! :)

Ok. I'm going to work through your code and come up with where you went wrong. Gimme a couple of minutes.
I used <list> because i didn't have access to your "list.cpp". From what I can see in your code, you aren't handling the oneline variable that you retrieve from getline. You need to parse this into your string first and last. I propose using something like stringstream to handle this. Also you should use !data.eof() instead of data in your while statement.

Here is what I had done for getList():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void getList (ifstream& data, list<string>& mylist)
{
    string first, last, result;
    string oneline;
    
    while (!data.eof())
    {
        getline (data, oneline);
        // Use stringstream to manipulate data in the line and parse to strings first, last
        stringstream ss (stringstream::in | stringstream::out);
        ss << oneline;
        ss >> first;
        ss >> last;
        result = parse2(first,last);
        mylist.push_back(result);
    }
}
Last edited on
Topic archived. No new replies allowed.