Printing out list elements issue
<p>I am writing a function that prints out items from a linked list. It is printing out things fine, but once it reaches the end and hits a node that was I think null or with no initial number, it prints out a random number (I'm guessing stored in the computer). How can I fix this?</p>

<pre><code>void printList(intNode*&amp; intList)
{
intNode* current;

if (intList==NULL)
{
    cout &lt;&lt; "No elements to print-list is empty." &lt;&lt; endl;
}
else
{
    cout &lt;&lt; "Elements in list:" &lt;&lt; endl;
    current = intList;
    while (current!=NULL)
    {
        cout &lt;&lt; current-&gt;intValue &lt;&lt;endl;
        current=current-&gt;nextNode;
    }
    if (current==NULL)
    {
        cout &lt;&lt; "End of list" &lt;&lt; endl;
    }
}
}
</code></pre>

<p>Here is where I create the list:</p>

<pre><code> void createList(intNode*&amp; intList)
 {
  intNode* lastInt; //points to last integer in file
  lastInt = NULL;
  int fileInt; //int read from input file

ifstream intInputFile;
intNode* anotherInt;
anotherInt = new intNode;

intInputFile.open("intInput.txt");
if (intInputFile.is_open())
{
    cout &lt;&lt; "intInput.txt open successful" &lt;&lt; endl;
    cout &lt;&lt; "check" &lt;&lt;endl;
    while(intInputFile&gt;&gt;fileInt)
    {
        if(intList==NULL)
        {
            intList = anotherInt;
            lastInt = anotherInt;
            lastInt-&gt;nextNode = NULL;
            lastInt-&gt;nextNode = new intNode;
        }
        else
        {
            lastInt = lastInt-&gt;nextNode;
            lastInt-&gt;nextNode = NULL;
            lastInt-&gt;nextNode = new intNode;
        }
        lastInt-&gt;intValue = fileInt;
        cout &lt;&lt; lastInt-&gt;intValue &lt;&lt;endl;
    }
    lastInt-&gt;nextNode-&gt;nextNode=NULL;
    intInputFile.close();
    cout &lt;&lt; "List created from input file" &lt;&lt; endl;
}
else
{
    cout &lt;&lt; "intInput.txt open unsuccessful" &lt;&lt; endl;
}
}
</code></pre>
