Extra Node with Link List

I'm developing a program that stores information about different companies in a link list. I must read the information in from a text file, and store it into a link list. I have an extra node that is appearing at the end of my data. I'm unsure if it is from reading it in or how I am displaying it. Here is my code:


At the end the empty strings and default value for ints. I'm unsure where I'm getting this issue or how it's occurring.
Last edited on
It's happening during the read. The file doesn't know it's reached the end until a failed read occurs. So it passes the "while (!eof)" test, then fails on the getline call, but you don't really test it there.
Line 11: you are looping on EOF, but you need to test for EOF after attempting to read data, not before. You create a new (empty) node before attempting to read potentially non-existing data. Don't do that.

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
void company::read_in(istream& filein)
{               
                NodePointer crt;
                assert( head == NULL );
                while (true)
                {
                                // This is our temporary input node
                                Node n;
                                filein >> ws;  // skip whitespace

                                // this is where we will ATTEMPT to get data
                                getline(filein, n.company);
                                getline(filein, n.phonenumber);
                                filein >> n.avg_salary;
                                filein >> n.mngr_salary;

                                // Now, ONLY if we succeeded, add a copy of the node to the list
                                if (!filein) break;

                                // You do have a copy ctr, right? We use it here as:
                                //   foo = new Node( n );
                                if (head == NULL)
                                                head = crt = new Node( n );
                                else
                                                crt->link = new Node( n );
                }

                // Frankly, the Node ctr should have done this for you already:
                if (head != NULL)
                                crt->link = NULL;
}

You have named your nodes variously "person" and "node" -- I named it "Node". Substitute the proper name. Make sure that your constructor properly initializes the fields of a Node:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Node::Node(
  const string& company = "",
  const string& phonenumber = "",
  double avg_salary = 0.0,
  double mngr_salary = 0.0,
  Node* link = NULL  // <-- this is IMPORTANT
  ):
  company( company ),
  phonenumber( phonenumber ),
  avg_salary( avg_salary ),
  mngr_salary( mngr_salary ),
  link( link )  // <-- this is IMPORTANT
  {
  }

Also, don't forget a copy constructor:

1
2
3
4
5
6
7
8
Node::Node( const Node& n )
{
  company = n.company;
  phonenumber = n.phonenumber;
  avg_salary = avg_salary;
  mngr_salary = mngr_salary;
  link = n.link;
}

Hope this helps.
Ohhhhh that makes a lot of sense. Thank you very much.
Topic archived. No new replies allowed.