i got a problem with my c++ code where i using struct to make variables to
my list. The program is to list users thats added (user1, user2), i can add user3 but! When i try to delete one of the users the program crash and some times it dosen't. The error i get dosen't help me at all so im asking you guys here for help :)
I ran it and used it and it runs perfectly, i ended up deleting all users, adding new users, listed all of the users and exit without any crashes. What exactly is the matter?
Also please try commenting the code so people like me dont have too scan through the code looking for whats going on. Because i dont understand the language that pops up when not in the main menu, so i had to search for what it was doing :) just a suggestion
erase() returns an iterator to the next element, your for loop will attempt to increment that iterator again, before evaluating the end condition.
1 2 3 4 5 6 7 8 9 10 11
for(list<userdata>::iterator it = datalist.begin(); it != datalist.end(); it++ /* remove */)
{
if((*it).username == anv)
{
it = datalist.erase(it); // the iterator is incremented here
}
else
{
it++; // only increment if there was no erase operation}
}