My code is below, and my problem occurs when I am trying to “loadFile” through my program. It asks me for the filename, and when I give it a filename that it can find, it just says “Segmentation Fault” and stops running. I think the problem is either coming from the loadFile method itself or on line 244 which is where I’m writing the method for adding an element to the linkedlist. Any help is appreciated! Thanks.
Line 224:
void countyElectionList::push_back(countyElectionResults * newCER){
if(head == NULL)
head = newCER;
else
{
countyElectionResults * current = head;
while(current->getNextResult() != NULL){
current = current->getNextResult();
}
current->setNextResult(newCER);
}
}
According to the source code at the URL you linked (which also will not compile for a number of reasons, including missing brackets and functions missing return statements, among other things) the problem is here:
You never initialize
countyElectionList::headto NULL, so when you callcountyElectionList::push_backthe value ofheadwill most likely not point to NULL but to who-knows-what… hence the crash.For future reference: when you debug crashes if the function in which the crash occurs doesn’t look suspicious and you can’t find a fault with the source code and in stepping through the logic step-by-step, you should move on to examining the caller(s) of the function.
Update: So, after examining your new pastebin code (which still does not compile, as several functions which claim to return values lack return statements) the error continues to be in the same function:
Notice that you using
==which is the comparison operator and not the assignment operator, which is=. This is the correct code to use:I have to wonder if you actually even tried to compile this code. I did, and my compiler immediately pointed the error out to me. It even told me what to do:
I also suspect that you are in way over your head. I am assuming that this is homework and you’re not doing an actual election system (although…) so I guess my next question is: have you considered talking to your professor and explaining how you having difficulty with the syntax of the language? Perhaps your school has resources that could help. Trying to program by randomly inserting stuff in a file doesn’t help you learn; it’s an exercise in frustration.