0

So I have this part of my code here

This is to loop 5 times (MAX = 5) to insert word into a link list.

for ( int i = 0 ; i < MAX ; i++ )
{
    string alphabet = G->returnAlphabets();

    L[i]->addWords(alphabet[i]);
}

And I have the insert word part here

string line;

fstream myfile ("Words.txt");

while (!myfile.eof())
{
    getline(myfile,line);

    if ( alphabet == line[0] )
    {
        ListNode *newNode = new ListNode;

        if ( head == NULL )
        {
            newNode->item = alphabet;           
            newNode->next = NULL;
            head = newNode;
        }

        else
        {
            newNode->item = line;               
            prev = cur;
            prev->next = newNode;
        }

        cur = newNode;
    }
}

myfile.close();

My problem is that it gives me error on the last loop. Meaning on the MAX = 4, the insert into node. The rest of the loop is fine. When I decrease the MAX to 4 meaning that it loop 4 times, the program is error-free but when the program loops for 5 times it gives error.

Anyone can point to me my mistakes?

Edit:

I've detected that during the last loop, it doesn't go to the first node.
And also the program get the word from the file just that it gives error when entering the first node

Poh Sun
  • 65
  • 2
  • 9
  • 1
    [`while (!eof())` is wrong.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – chris Aug 26 '13 at 03:53
  • What's wrong with it? Can elaborate? – Poh Sun Aug 26 '13 at 03:54
  • It isn't complete and won't compile or run? Thats a good place to start. Ex. What is `L` in the first snippet. – WhozCraig Aug 26 '13 at 03:56
  • It runs but give me error on the last loop. The rest of the loop is okay. – Poh Sun Aug 26 '13 at 03:56
  • Not the version ***we*** have. Look away from your code window and look at what you *posted*. Try and see it from *our* side and then post [something that exhibits the problem](http://www.sscce.org). – WhozCraig Aug 26 '13 at 03:57
  • 1
    @PohSun, An error on the last loop sounds exactly like an effect of what I mentioned. The link explains it pretty well. – chris Aug 26 '13 at 03:58
  • @chris Will look it out because it works before. But when I add something it gone wrong. I revert back but it gives me error now. Thanks for pointing it out. – Poh Sun Aug 26 '13 at 04:04

1 Answers1

1

The logic using while (!myfile.eof()) is bad because you do not check that getline(myfile, line); succeeded, and then proceed to use the value when getline() detected EOF. You should be using:

while (getline(myfile, line))
{
    ...rest of loop...
}

It is not clear (to me) without scrutinizing the manuals or standard what the state of line is after a failed getline(), but it could easily lead to line[0] failing.

Since you say "it is failing on the last iteration", this could be the principal cause of your trouble.

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
  • Well. Tried your solution but still the program gives me error. Well, when I try with the MAX = 4 meaning that I loop for 4 times it works. But when loop for 5 times it gives me error – Poh Sun Aug 26 '13 at 05:24
  • OK - then we need (a) your code and (b) your data. Without both, it is hard to be sure what your trouble is. Please study how to create an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)). – Jonathan Leffler Aug 26 '13 at 05:56