Help with getting an item in a list

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
#include <iostream>
#include <cstring>
#include <string.h>
#include "Item.h"
#include "Character.h"

using namespace std;

#pragma once;

class CharacterList
{
private:
	Character *m_pHead;

public:
	CharacterList();
	~CharacterList();
	bool addCharacter(Character *newCharacter);
	Character *deleteCharacter(char *characterName);
	bool addItem(char *characterName, Item *newItem);
	Item *getItem(char *characterName, char *itemName);
	Item *dropItem(char *characterName, char *itemName);
	void printCharacters();

};

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <cstring>
#include <string.h>
#include "Item.h"
#include "CharacterList.h"
#include "Character.h"

using namespace std;

CharacterList::CharacterList()
{
	m_pHead = NULL;
}

CharacterList::~CharacterList()
{

}

bool CharacterList::addCharacter(Character *newCharacter)
{
	Character *temp = m_pHead;
	if (temp == NULL)
	{
		m_pHead = newCharacter;
		return true;
	}
	while(temp->m_pNext !=NULL)
	{
		temp= temp->m_pNext;
	}
	temp->m_pNext = newCharacter;
	return true;

}

Character CharacterList::*deleteCharacter(char *characterName)
{



}

bool CharacterList::addItem(char *characterName, Item *newItem)
{
	Character *temp = m_pHead;
	while(temp != NULL)
	{
		if(strcmp(characterName, temp->getName()) == 0)
		{
			return temp->addItem(newItem);
		}
		else
		{
			temp = temp->m_pNext;
		}
	}
}

Item CharacterList::*getItem(char *characterName, char *itemName)
{
	
	Character *temp;
	while(temp != NULL)
	{
		if(strcmp(characterName, temp->getName()) == 0)
		{
			return temp->*getItem(itemName);
		}
		else
		{
			temp = temp->m_pNext;
		}
	}
}

Item CharacterList::*dropItem(char *characterName, char *itemName)
{

}

void CharacterList::printCharacters()
{
	Character *temp = m_pHead;
	while (temp != NULL)
	{
		temp->printAll();
		temp = temp->m_pNext;
	}

};


1
2
3
4
5
6
7
8
9
10
11
12
#ifndef ITEM_H
#define ITEM_H

struct Item
{
	char	m_sItemName[65];
	int		m_iType;
	double	m_dValue;
	double	m_dWeight;
};

#endif 


QUESTION: Item *getItem(char *characterName, char *itemName)--this function takes a player name, and the name of an item. It will then locate this character in the list and search for the named item in the character's list of carried items. It will return a pointer to the item if found or NULL if not found. If the character was not found in the list it should also return NULL.
HELP with line 60-75
Last edited on
Hi,

Have you heard of std::string ? This seems like a classic case of using C programming techniques to reinvent a std::list<char>, which could have just been a std::string. Sorry if I am pouring cold water all over your code, maybe it is one of those terrible assignments where the teacher wants to do OOP, but has a shocking lack of imagination?

Also, consider dropping the Hungarian Notation : iType that was a thing that was popular in the early 1990's and resulted in some thoroughly abhorrent code back then.

And there is a school of thought that the m_ isn't necessary for a member variable. I find it easier to rename the parameters to any function, to the member name with Arg appended. So for example a Person class constructor might have a parameter:
const std::string& PassportIDArg

where the member variable name is just PassportID

If this isn't an assignment, my apologies again :+) , but try really hard to get away from ideas you may have had from learning C. C++ really is quite different, perhaps surprisingly so. See what you can do with std::string, then have a go with std::vector .

Good Luck !!
Topic archived. No new replies allowed.