#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
#include <algorithm>
using namespace std;
class item {
public:
string name;
int damage;
int heal;
};
class enemy {
int health;
};
int main()
{
item sword{ "sword", 5, 0 };
item fist{ "fist", 1, 0 };
vector <item> inventory;
string input;
item weapon = fist;
item head;
item chest;
item legs;
item feet;
while (true) {
cin >> input;
if (input == "getsword") {
inventory.push_back(sword);
cout << "\nSword added to inventory \n";
}
if (input == "inventory") {
for (std::vector<item>::iterator i = inventory.begin(); i != inventory.end(); i++) {
std::cout << i->name << ", ";
}
}
if (input == "equip") {
cout << "Which item would you like to equip";
cin >> input;
if (input == "sword") {
if(find(inventory.begin(), inventory.end(), sword) != inventory.end()) {
weapon = sword;
cout << "sword equipped";
}
}
}
}
}
Hello, I'm having some trouble trying to find if an object is in a vector. I've tried many solutions with pointers (which I'm having trouble understanding) and I can't seem to get any of them to work.
if(find(inventory.begin(), inventory.end(), sword) != inventory.end())
Any help would be appreciated, thanks.