Inventory & Item IDs

I'm practicing some C++ and was wondering what the best approach is to creating a basic Inventory System, where item's have their own ID assigned to them. I'm trying to expand on my C++ skills, so I am making small little programs for practice.

I've created a class that holds the ItemName & ItemID. A Getter and Setter have been added to get the items ID, but I seem to be struggling on what to do next and how I assign an item with an ID number. For example, saying Silver Key has an item ID of 3.

Also, when storing this in an inventory what type of Array would be best to use? I have done some previous research on Vector Arrays and Dynamic Arrays, but forgotten how to implement them. I can research what is the best one to use and try and implement it myself once I know which one is best to use.

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
#include "stdafx.h"
#include <iostream>
#include "ItemIDS.h"
#define INVENTORY 6
using namespace std;

class ItemHandler
{
public:
	int ItemID;
	string ItemName;
	int ItemCapacity;

	void setItemID(int);
	int getItemID();
};

void ItemHandler::setItemID(int n)
{
	ItemID = n;
}

int ItemHandler::getItemID()
{
	return ItemID;
}

int main()
{
	ItemHandler ih;
	ih.getItemID;
	int inventory[INVENTORY] = { 1,0,0,0,0,0 };
	for (int i = 0; i < INVENTORY; i++)
	{
		cout << "Inventory Contains: " << inventory[i] << endl;	
	}
	return 0;
}
Last edited on
Hello George1993,

One thing that would help is to post the code you have written instead of letting everyone guess at what you have done. Someone might see something you have done wrong or that could be improved on.

Arrays based on the class for the type will work. I tend to use "std::vector"s over arrays. With a vector it will hold only what you need and getting the amount of elements in the vector is just asking the right question "vectorName.size()". Note the return value is an "unsigned int".

I find myself quite often referring to http://www.cplusplus.com/reference/vector/vector/ for help or ideas regarding member functions that I do not use often.

Hope that helps,

Andy
Don't know if this helps but you can study...

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
#include<iostream>
#include<string>
#include<vector>
using namespace std;

class inventory {
	string name;
	int id;
	int quantity;

public:

	inventory();

	void setName(string s) {
		name = s;
	}

	string getName() {
		return name;
	}

	void setId(int i) {
		id = i;
	}

	int getID() {
		return id;
	}

	void setQty(int i) {
		quantity = i;
	}

	int getQty() {
		return quantity;
	}

};

inventory::inventory() {
}//empty constructor

void printInventory(vector<inventory> v) {
	for (int i = 0; i < v.size(); i++) {
		cout << "Name: " << v[i].getName() << "  ID: " << v[i].getID() << "  Quantity: " << v[i].getQty() << endl;
	}
}



int main() {

	vector<inventory> myInventory;
	inventory a; //empty inventory item
	
	
	myInventory.push_back(a);
	myInventory[0].setName("straw");
	myInventory[0].setId(100);
	myInventory[0].setQty(2);
	
	myInventory.push_back(a);
	myInventory[1].setName("wood");
	myInventory[1].setId(200);
	myInventory[1].setQty(5);

	myInventory.push_back(a);
	myInventory[2].setName("bricks");
	myInventory[2].setId(300);
	myInventory[2].setQty(1000);
	
	printInventory(myInventory);
	
	system("pause");


	return 0;
}
Thank you for the example, it really helped!

In terms to assigning the items with the method used, is there a way to set up item IDs in a separate file, and then call that in the main file and add it to the inventory.

For example, if a player wishes to pick up a key, with an item id of 1, can that be added to the inventory by calling another function or something that holds the details for the key?
Well it sounds like maybe you need two inventory vectors, one that stores all possible inventory, and one that stores the players current inventory.

You could do something like this...

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
92
93
94
95
#include<iostream>
#include<string>
#include<vector>
using namespace std;

class inventory {
	string name;
	int id;
	int quantity;

public:

	inventory();

	void setName(string s) {
		name = s;
	}

	string getName() {
		return name;
	}

	void setId(int i) {
		id = i;
	}

	int getID() {
		return id;
	}

	void setQty(int i) {
		quantity = i;
	}

	int getQty() {
		return quantity;
	}

};

inventory::inventory() {
}//empty constructor

void printInventory(vector<inventory> v) {
	for (int i = 0; i < v.size(); i++) {
		cout << "Name: " << v[i].getName() << "  ID: " << v[i].getID() << "  Quantity: " << v[i].getQty() << endl;
	}
}

inventory search(vector<inventory> v, int id) { //search by id number
	for (int i = 0; i < v.size(); i++) {
		if (v[i].getID() == id) {
			return v[i];
		}
	}
}

void addToPlayer(vector<inventory> &player, vector<inventory> v, int id) {
	player.push_back(search(v, id));
}



int main() {

	vector<inventory> myInventory;
	vector<inventory> playerInventory;

	inventory a; //empty inventory item
	
	
	myInventory.push_back(a);
	myInventory[0].setName("straw");
	myInventory[0].setId(100);
	myInventory[0].setQty(2);
	
	myInventory.push_back(a);
	myInventory[1].setName("wood");
	myInventory[1].setId(200);
	myInventory[1].setQty(5);

	myInventory.push_back(a);
	myInventory[2].setName("bricks");
	myInventory[2].setId(300);
	myInventory[2].setQty(1000);
	
	addToPlayer(playerInventory, myInventory, 200);

	printInventory(playerInventory);
	
	system("pause");


	return 0;
}
I've been messing around with this code for a few days and understand what's going on (to a degree).
I've mesmerized the code and typed it out pretty much how it appears above, but when I go to run the program, all I get is
1
2
C4018 '<': signed/unsigned mismatch (Line 55)
C3867 'inventory::getName': non-standard syntax; use '&' to create a pointer to member (Line 56)


Program code:
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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class inventory {
private:
	string itemName;
	int itemID;
	int capacity;
	int quantity;
public:
	inventory();

	void setName(string s) {
		itemName = s;
	}

	string getName() {
		return itemName;
	}

	void setID(int i) {
		itemID = i;
	}

	int getID() {
		return itemID;
	}

	void setCapacity(int i) {
		capacity = i;
	}

	int getCapacity() {
		return capacity;
	}

	void setQuantity(int i) {
		quantity = i;
	}

	int getQuantity() {
		return quantity;
	}
};

inventory::inventory() {
	//Epmty
}

void printInventory(vector<inventory> v) {
	for (int i = 0; i < v.size(); i++) {
		cout << "Name: " << v[i].getName << endl;
	}
}

int main()
{
	vector<inventory> pInventory;
	inventory a;

	pInventory.push_back(a);
	pInventory[0].setName("Item Test 1");


	pInventory.push_back(a);
	pInventory[1].setName("Item Test 2");

	printInventory(pInventory);
    return 0;
}




Edit: Fixed the problem, forgot the () after .getName
Last edited on
Topic archived. No new replies allowed.