#Inventory (vector) crashes without any error?

8 messages · Page 1 of 1 (latest)

cinder comet
#

when i take the last item or the inventory is empty and i try to equip something my code crashes without any error?

drowsy havenBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

cinder comet
#

Here is my inventory equip code: ```cpp
while (true) {
clear_screen();
user.show_equipment();
inventory.displayItems();
slow_print("[1] Equip |[2] Remove |[3] Go back\n> ");
std::cin >> choice;
if (choice == 1) {
clear_screen();
user.show_equipment();
inventory.displayItems();
int eq;
slow_print("What item do you want to Equip?\nID > ");
std::cin >> eq;
if (std::cin.fail()) {
std::cin.clear(); std::cin.ignore(std::numeric_limitsstd::streamsize :: max(), '\n');
} else {
user.equip(inventory.getItem(eq));
inventory.remove_item(eq);
}

#

error is this: ```cpp
user.equip(inventory.getItem(eq));

#
void Player::equip(Item* _item_id) {
    if (_item_id->get_categorie() == 'S') {
        weapon_slot = _item_id->get_name();
        normal_damage = _item_id->getDamage();
    }
    if (_item_id->get_categorie() == 'A') {
        armor_slot = _item_id->get_name();
        normal_defense = _item_id->getDefense();
    }
}

or

Item* Inventory::getItem(std::size_t index) {
    if (index < inventory.size()) {
        return inventory[index];
    }
    return nullptr;
}
#

and i cant explain me why this is so

#

got it

#

!solved