c++ - Linked List error when inserting for the last time -
so have part of code here
this loop 5 times (max = 5) insert word link list.
for ( int = 0 ; < max ; i++ ) { string alphabet = g->returnalphabets(); l[i]->addwords(alphabet[i]); }
and have 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 gives me error on last loop. meaning on max = 4, insert node. rest of loop fine. when decrease max 4 meaning loop 4 times, program error-free when program loops 5 times gives error.
anyone can point me mistakes?
edit:
i've detected during last loop, doesn't go first node.
, program word file gives error when entering first node
the logic using while (!myfile.eof())
bad because not check getline(myfile, line);
succeeded, , proceed use value when getline()
detected eof. should using:
while (getline(myfile, line)) { ...rest of loop... }
it not clear (to me) without scrutinizing manuals or standard state of line
after failed getline()
, lead line[0]
failing.
since "it failing on last iteration", principal cause of trouble.
Comments
Post a Comment