-1

Here is my original post: Program crashes after opening file

I've tried again and again to fix my code, but it still either crashes or runs uncontrollably. I have yet to find a solution.

Here is my updated code:

while(!intInputFile.eof())
{
   intNode* anotherInt;
   anotherInt = new intNode;
   if(intList==NULL)
   {
       intList = anotherInt;
       lastInt = anotherInt;
   }
   else
   {
      lastInt->nextNode = new intNode;
      lastInt = lastInt->nextNode;
      lastInt->nextNode = NULL;
   }
   lastInt->intValue = fileInt;
   lastInt = lastInt->nextNode;
   lastInt->nextNode = NULL;
   intInputFile >> fileInt; // *** Problem occurs on this line. ***
}
Community
  • 1
  • 1
A A
  • 187
  • 7
  • 2
    [Please don't use `eof()` in the loop condition](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – chris Dec 19 '12 at 04:38
  • @chris The algorithm was provided by my instructor, so I had to use it. – A A Dec 19 '12 at 04:39
  • 5
    Show him/her that question. Please, for the sake of us all. – chris Dec 19 '12 at 04:42
  • Lol. I mean is it causing the crash? – A A Dec 19 '12 at 04:48
  • Probably not, but it is buggy, and I have seen weird things result from using it. I'd look for dereferencing null pointers and making sure your `intNode` class functions the way it should. – chris Dec 19 '12 at 04:51
  • You can use gdb to debug.find the source code line that the crash come up. – bbg Dec 19 '12 at 04:59
  • @AA Just because something doesn't cause a crash doesn't imply it's perfect code. – Thomas Dec 19 '12 at 05:43

1 Answers1

2

The problem is you are updating lastInt twice:

else
{
   lastInt->nextNode = new intNode;
   lastInt = lastInt->nextNode;
   lastInt->nextNode = NULL;
}

lastInt->nextNode is now NULL

lastInt->intValue = fileInt;
lastInt = lastInt->nextNode;

now lastInt is NULL

lastInt->nextNode = NULL;

now you're dereferencing a null pointer and causing an exception. You should not be updating lastInt after the else.

Eli Algranti
  • 7,922
  • 2
  • 35
  • 47
  • I fixed it, but am still getting a runtime error. I've tried everything, I don't see what the problem is honestly. 'else { lastInt->nextNode = new intNode; lastInt = lastInt->nextNode; lastInt->nextNode = NULL; } lastInt->intValue = fileInt; lastInt = lastInt->nextNode; intInputFile >> fileInt; ' – A A Dec 19 '12 at 05:03
  • remove lastInt = lastInt->NextNode as well. If not its going to fail on the next loop inside the else. You want it to point to the last node in the linked list not to null. – Eli Algranti Dec 19 '12 at 05:16
  • Okay, I removed it, but it still doesn't work. Could there be something wrong with my file? Although it's only a file of numbers. – A A Dec 19 '12 at 05:27
  • Could be. What error exactly are you getting? Don't you have a debugger to run through your code. I can't see anything else wrong, but I have not run your code. – Eli Algranti Dec 19 '12 at 05:55
  • I put a `cout <>fileInt;` and when I run my program, it just says "good" repeatedly, nonstop. I don't understand why though. – A A Dec 19 '12 at 06:03