Linked List Help. Elements with the same name?

Hello all, this is my first post here so let me know if I did not provide enough information!

Basically I'm creating an inventory list where a player can pick up and drop things on the ground. I have linked lists in every location on the board.

Some areas carry the same items, for example: "Gold" can be found in one area or in another. When I drop multiple items in one spot it will look like this: "Gold, Gold, Berries, Gold".

The first two times I pick it up, it is perfectly fine and on the ground will show "Berries, Gold".
The problem is when I try to pick up another item it gives off my error message, which is only supposed to go off when there is nothing on the ground... but there is.

Here is my code for picking up items:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//Pick up an item on the ground
void Location::pickUpItem(Player &p) {
	string choice;
	Item *current = theGround.getFirstElement();
	//Check the ground to see if there is an item there
	if (theGround.getFirstElement() != 0) {
		//Display what is on the ground
		cout << endl << "There is an item on the ground! You look closer and see: " << endl
			<< "(Note for multiple items it will show twice) " << endl;

		while (current != 0) //end when there are no more elements in the list
		{
			//Get the name of the item, then get the name of the next
			cout << current->getName() << endl;
			current = current->getNext();
		}
		cout << endl << "Do you wish to pick it up? First item will get picked. (y/n)" << endl;
		cin >> choice;

		if (choice == "y" || choice == "Y") {
			current = theGround.getFirstElement();//Make a temp value to store the element
			choice = current->getName();
			p.putInInventory(current);//Pick up the item to put in inventory
			while (current != 0) //end when there are no more elements in the list
			{
				//Get the name of the item, then get the name of the next
				if (current->getName() == choice) {
					string temp = choice;
					theGround.remove(temp);//Remove this item from the ground
					break;
				}
				current = current->getNext();
			}
			
			cout << "You now have \"" << choice << "\" in your inventory!" << endl;
		}
	}
}


Here is my linked list remove function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//To remove an element in the list
Item *LinkedList::remove(string key) {

	//CASE 1: List is empty
	if (head == 0) {
		return 0;
	}

	//CASE 2: Remove the head 
	else if (key == head->getName()) {
		Item *temp = head;//Save the value
		head = head->getNext();//Will point to the next list element

		temp->setNext(0);//Make sure that the deleted element should not point to anything in the list

		return temp;
	}

	//If the element is in the other nodes
	else {
		Item *current = head;

		while (current->getNext() != 0) {
			if (key == current->getNext()->getName()) {//This will choose the one before the desired deleted element
				Item *temp = current->getNext();//Make a temp value for the next element
				current->setNext(temp->getNext());//Will set the one before to equal the one after the desired deleted element

				temp->setNext(0);//Make sure that the deleted element is not pointed to anything else

				return temp;
			}

			current = current->getNext();//Will iterate through the nodes

		}
	}

	return 0;
}


I just do not understand why my list is cutting down on two short, any help is much appreciated!!
Last edited on
The program runs just fine, with no compiler error.
It's just that after I pick up two items from the ground, whatever that was left on the ground suddenly disappears like nothing else was there.
Topic archived. No new replies allowed.