Okay, maybe I'm just overlooking something but I need help. I have this program that reads in some numbers from a file and makes a linked list. The only function that is not working properly is one I called IsThere. Basically, it's just supposed to tell me whether or not a specific number is in the list. It works fine as long as the number is in the list but if it is not, the program crashes. I can't figure out what I'm doing wrong. Below is the IsThere function from my implementation file and then I'll also include the line of code from my client code.

bool  ItemList::IsThere(ItemType  item) const
{

      NodePtr  newIsTherePtr;
      newIsTherePtr = listPtr;
      while (newIsTherePtr->item!=item)
      {
       newIsTherePtr=newIsTherePtr->next;
       }
      if(item==newIsTherePtr->item)
      {
       return true;
      }
      else
      {
       return false;   
      }

}
cout<<"Is 2000 an item in the list? "<<temp.IsThere(2000)<<endl;
cout<<"Is 78 an item in the list? "<<temp.IsThere(78)<<endl;

Note: 2000 is in the list so if I get rid of the second line, the program works fine. However, if I put a number in that is not in the list, the program crashes at this point. I know it has something to do with how I coded the function but I'm outta ideas...

while (newIsTherePtr->item!=item) needs another condition to ensure you don't run right off the end of the list (e.g., && it with newIsTherePtr->next !=NULL or however you designate the end of your list)

:sad: Geez, I hate when I do that...i actually had that second condition on there in one of my tries but forgot the "->item" part. As always, thanks for your help!

while (newIsTherePtr->item!=item) needs another condition to ensure you don't run right off the end of the list (e.g., && it with newIsTherePtr->next !=NULL or however you designate the end of your list)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.