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