Issues with a inventory system

I am creating an inventory system for a school project however i have hit a snag

i have declared vector<string> inventory; declared in a global.h file, as well as a bool Check_Inv(string);

in the the global is active in all my cpp files for i am doing a multi level game.

in the first of the levels i have the bool function declared

bool Check_Inv(string check)
{
for(int i = 0; i < inventory.size();i++)
{
string item = inventory[i];
if (item == check)
return true;
}
}

and then i have my push_backs and my checks set to see if the item is in the inventory.

unfortunately i get this error when trying to build

Error 6 error LNK2005: "struct Vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > inventory" (?inventory@@3U?$Vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@A) already defined in Bridge.obj C:\Users\Dale\Desktop\C++\Key To Oblivion\Key To Oblivion\Cabin.obj Key To Oblivion

even my teacher had a tough time trying to figure it out before class ended.

any help that can be given is appreciated
You should not have something like this: vector<string> inventory; in a header (.h) file.

You need to make a global.cpp file and put it in there.
In the global.h file you need to do this: extern vector<string> inventory;

Make sure you #include your global.h in your global.cpp file.
Last edited on
That helped the program run, however i am now experiencing an error where the item appears to be already in the inventory before the push_back is set

the push_back is within a switch used for making choices in the game

switch(choice)
{
case 1:
{
if(Check_Inv("Card"))
{
bridge();
}
else
{
cout<<"The door will not open\n";
}

break;

}
case 2:
{
if(Check_Inv("Bar"))
{
cout<<"Inside the cabinet you find a keycard....\nit could be used to open the door\n\n";
inventory.push_back("Card");
}
else
{
cout<<"The cabinet is jammed and will not open\n\n";
}
break;

}
case 3:
{
cout<<"You see an endless field of stars slowly moving by\n\n";
break;
}
case 4:
{
inventory.push_back("Bar");
cout <<"You have collected a Metal Bar\n\n";
break;

}
case 5:
{
inventory.push_back("Tbrush");
cout<<"You have collected a Tooth Brush with the number 42 engraved on it\n\n";
break;

}
case 6:
{
cout<<"You climb back into the sleeping chamber and decide to rest a while\n\n";
break;
}
defalt:break;
is there an adjustment i can make to ensure it isn't in the inventory until the choice is made

idealy choice 1 shouldn't work unless i have already done choice 2. Also choice 2 shouldn't work unless i already have done choice 4....
Doesn't your Check_Inv() function do that? How have you implemented it?
I did implement the fucnction. But instead of waiting till the push back places the "item" into the vector, it seems to automatically be there

as well if i try to cout the Check_Inv() i get 00F9126C displayed instead of the items that have been pushed back into the vector
the implementation is :

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
bool Check_Inv(string check)
 {
	 for(int i = 0; i < inventory.size();i++)
	 {
		 string item = inventory[i];
		 if (item == check)	
			 return true;
	 } 
 }

bool cabin()
{
	player.SetName("");
	
	cout<<"What is your name?\n";
	char name[32];
	cin >> name;
	player.SetName(name);

	cout<<"\n\n";

	
	int choice =0;
	
	while(choice != 10)
	{	
	
		cout<< "You find yourself inside what seems to be a bedroom.\nThe bed capsule you awoke from is behind you.\nTo the left is a door and a cabinet.\nTo your right what seems to be a bathroom...it even has a toothbrush.\nIn front a window showing a star system and a small metal bar on the floor.\n\n";
		
		cout<< "What shall you do "<< name<<"?\n\n"; 
	
		cout<< "1: Try Door\n";
		cout<< "2: Try Cabinet\n";
		cout<< "3: Look Out Window\n";
		cout<< "4: Grab Metal Bar\n";
		cout<< "5: Grab Toothbrush\n";
		cout<< "6: Use Bed\n";
	
		cout<< "9: Check Current Inventory\n";
		cout<< "10: Exit Game\n";
		cout << ">";

		cin>> choice;
		cin.ignore(10,'\n');

		cout<< endl;

		switch(choice)
		{
		case 1:
			{
				if(Check_Inv("Card"))
				{
				 bridge();
				}
				else
				{
					cout<<"The door will not open\n";
				}
				
				break;
				
			}
		case 2:
			{
				if(Check_Inv("Bar"))
				{
					cout<<"Inside the cabinet you find a keycard....\nit could be used to open the door\n\n";
				inventory.push_back("Card");
				}
				else
				{
					cout<<"The cabinet is jammed and will not open\n\n";
				}
				break;
				
			}
		case 3:
			{
				cout<<"You see an endless field of stars slowly moving by\n\n";
				break;
			}
		case 4:
			{
				inventory.push_back("Bar");
				cout <<"You have collected a Metal Bar\n\n";
				break;
				
			}
		case 5:
			{
				inventory.push_back("Tbrush");
				cout<<"You have collected a Tooth Brush with the number 42 engraved on it\n\n";
				break;
				
			}
		case 6:
			{
				cout<<"You climb back into the sleeping chamber and decide to rest a while\n\n";
				break;
			}
		case 9:
			{
				cout<<"Your current inventory is:\n"<< Check_Inv <<"\n";
			}
		default:break;
		}
	}
	return true;
}


once this is working properly my and my partners game will be practically completed

BTW :your initial help allowed me to get the game running at its current state for which i am thankful
It looks like your Check_Inv() function didn't return false on failure.

1
2
3
4
5
6
7
8
9
10
11
12

bool Check_Inv(string check)
{
	for(int i = 0; i < inventory.size(); i++)
	{
		string item = inventory[i];
		if(item == check)
			return true;
	}
	// need to return false here
	return false;
}
Last edited on
That got it working thanks

i do have a question about that, i want to set it up so that once you have the item you can't try and get it again

is there a way i can check to see if that specific item is in the inventory and if it is make the choice no longer available

someone mentioned suing a bool "" = true; bool "" = false; set up

if i can figure this part out i believe i can be ready for the end game coding


You could put an if() statement around where you output the choices:
1
2
3
4
if(!Check_Inv("Bar"))
{
    cout<< "4: Grab Metal Bar\n";
}


This doesn't make any sense in C++: bool "" = true; bool "" = false;
i am sorry the "" what ever title i gave the bool. Such as bool barcomplete = false; and then in an if statement bool barcomplete = true;

if that doesn't apply to that instance, would it apply to checking if certain areas of the game are complete? therefore if an area complete bool = true and another = false then this ending occurs?
Topic archived. No new replies allowed.