Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8825181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:49:03+00:00 2026-06-14T06:49:03+00:00

I am having an issue getting a vector-based inventory system to work. I am

  • 0

I am having an issue getting a vector-based inventory system to work. I am able to list the items in the inventory, but not able to allow a user-selected item to be accessed. Here is the code:

struct aItem
{
    string  itemName;
    int     damage;

    bool operator==(aItem other)
    {
        if (itemName == other.itemName)
            return true;
        else
            return false;
    }
};

int main()
{
    int selection = 0;


    aItem healingPotion;
    healingPotion.itemName = "Healing Potion";
    healingPotion.damage= 6;

    aItem fireballPotion;
    fireballPotion.itemName = "Potion of Fiery Balls";
    fireballPotion.damage = -2;

    aItem testPotion;
    testPotion.itemName = "I R NOT HERE";
    testPotion.damage = 9001;
    int choice = 0;
    vector<aItem> inventory;
    inventory.push_back(healingPotion);
    inventory.push_back(healingPotion);
    inventory.push_back(healingPotion);
    inventory.push_back(fireballPotion);

    cout << "This is a test game to use inventory items. Woo!" << endl;
    cout << "You're an injured fighter in a fight- real original, I know." << endl;
    cout << "1) Use an Item. 2) ...USE AN ITEM." << endl;

switch (selection)
    {
    case 1:
        cout << "Which item would you like to use?" << endl;
        int a = 1;
        for( vector<aItem>::size_type index = 0; index < inventory.size(); index++ ) 
        {

            cout << "Item " << a << ": " <<  inventory[index].itemName << endl;
            a+= 1;
        }
        cout << "MAKE YOUR CHOICE." << endl << "Choice: ";

        cin >> choice;

^^^^
Everything above this line, works. I assume that my problem is the if statement, but I cannot figure out where I am going wrong in my syntax, or if there is a better way to do what I am doing.

        if (find(inventory.begin(), inventory.at(choice), healingPotion.itemName) != inventory.end())
            cout << "You used a healing potion!";
        else
            cout << "FIERY BALLS OF JOY!";
        break;

    case 2:
        cout << "Such a jerk, you are." << endl;
            break;

    }

EDIT: I think I’m not representing this correctly. I need for the player’s choice to affect the message displayed. Here’s a sample output of the 1st snippet:

Item 1: Healing Potion
Item 2: Healing Potion
Item 3: Healing Potion
Item 4: Potion of Fiery Balls

MAKE YOUR CHOICE. 
Choice: 

From there, the player can type 1-4, and what I would like is for the number (minus 1, to reflect the vector starting at zero) to be passed to the find, which would then determine (in this small example) if the item at inventory[choice – 1] is a healing potion. If so, display “You used a healing potion!” and if it is not, to display “Fiery balls of joy”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:49:04+00:00Added an answer on June 14, 2026 at 6:49 am

    Instead of:

    case 1:
            cout << "Which item would you like to use?" << endl;
            int a = 1;
            for( vector<aItem>::size_type index = 0; index < inventory.size(); index++ ) 
            {
    
                cout << "Item " << a << ": " <<  inventory[index].itemName << endl;
                a+= 1;
            }
            cout << "MAKE YOUR CHOICE." << endl << "Choice: ";
    
            cin >> choice;
            if (find(inventory.begin(), inventory.at(choice), healingPotion.itemName) != inventory.end())
                cout << "You used a healing potion!";
            else
                cout << "FIERY BALLS OF JOY!";
            break;
    
        case 2:
            cout << "Such a jerk, you are." << endl;
                break;
    
        }
    

    I neglected to realize that one of the wonders of vectors is the ability to access the value directly- Ryan Guthrie mentioned this in his comment, but I found a simpler “answer”. Namely:

    case 1:
        cout << "Which item would you like to use?" << endl;
        //TODO: Learn what the hell the following line actually means.
        for( vector<aItem>::size_type index = 0; index < inventory.size(); index++ ) 
        {
            //Makes a numerical list.
            cout << "Item " << index + 1 << ": " <<  inventory[index].itemName << endl;
            a+= 1;
        }
        cout << "MAKE YOUR CHOICE." << endl << "Choice: ";
    
        cin >> choice;
        //Cannot define this outside of the statement, or it'll initialize to -1
        invVecPos = (choice - 1);
    
        //This checks for an invalid response. TODO: Add in non-int checks. 
        if ((invVecPos) >= inventory.size())
        {
            cout << "Choice out of bounds. Stop being a dick." << endl;
        }
        //If the choice is valid, proceed.
        else
        {
            //checking for a certain item type.
            if(inventory[invVecPos].itemType == "ITEM_HEALTHPOT")
            {
                cout << "You used a healing potion!" << endl;
                //this erases the potion, and automagically moves everything up a tick.
                inventory.erase (inventory.begin() + (invVecPos));
            }
    
            else if(inventory[invVecPos].itemType == "ITEM_FIREPOT")
            {
                cout << "FIERY BALLS OF JOY!" << endl;
            }
    
            else
            {
                //error-handling! Whee!
                cout << "Invalid Item type" << endl;
            }
        }
    
    
        break;
    case 2:
        cout << "Why do you have to be so difficult? Pick 1!" << endl;
        break;
    

    Thank you, Ryan- with your prodding, I was able to look elsewhere and find the code I needed! The “fixed” code is commented heavily, so anyone else who runs into issues should be able to glean what they need!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having an issue getting a LINQ query to work. I have this XML:
I'm having an issue getting some code to work. Is there a way to
I'm having an issue getting this CSS for a sprite sheet to work on
I'm having an issue getting Appstats to work correctly. Using /appstats or /appstats/stats ends
So im having a little issue getting my menu with a slidedown function work
having an issue getting this to work. MySQL keeps throwing the following: ERROR 1064
I'm having an issue getting the following syntax to work: <rewriter> <!-- This does
I am having an issue getting bounds to work with my google map. All
I'm having an issue getting a regex field validator to work for an asp
I've been having an issue getting .getScript to work in some odd cases. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.