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.
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;
bool Check_Inv(string check)
{
for(int i = 0; i < inventory.size();i++)
{
string item = inventory[i];
if (item == check)
returntrue;
}
}
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;
}
}
returntrue;
}
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
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?