Okay, now I have no output. Parsing help!

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!
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
=//******************************************************************************
//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;           
       }
       
     

What is going on with Line 85? Check your argument carefully.

BTW, have you learned how to use a debugger yet?
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...

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
//******************************************************************************
//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;           
       }
 
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?
Last edited on
I did mine like this. You forgot to actually create what goes inside of first and last.

string parseInputString (string fullName)
{
       string newName;   
       string firstName, lastName;   
       int index;
       
       index = fullName.find(' ');
       firstName = fullName.substr(0, index);
       fullName = fullName.substr(index + 1, fullName.length() - 1);
       lastName = fullName;
       newName = lastName + ", " + firstName + ".";
       return newName;           
}
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.
Topic archived. No new replies allowed.