Modify the original Hero's Inventory I assignment to allow the user to create an inventory which not only holds the name of the objects obtained, but their point values as well. You will do this using a single structure, so that string names and numeric point values may be housed in a single structure. As the items are used, they are removed from the inventory. The avatar may also decide to "trade" an element from his list for a more desirable item. Please add a score variable, so that as the user adds items to this inventory, you add the proper number of points to their score.


Create a program which will create a Hero's Inventory. The program should take the user input (a number) and put that item into the inventory (for example, if the user enters 1. Sword, the program should insert the string "Sword" into the inventory). You will need to add a loop that allows the user to keep adding items into the inventory until it reaches a maximum number of list items, or the user decides to terminate adding.The program should also allow the user to discard/replace an item. If the user chooses to discard an item, the number of points should be deducted from their score.You will need two menus, one with items the user can add to their inventory, and another menu which lists actions they can take 1-Add Item, 2-Delete Item, 3-Swap Item.Everytime there is a change to the inventory, either an item is added, deleted, or swapped, the new inventory/points and total should print.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include <iostream>
#include <string>

using namespace std;

int main()
{
   
   unsigned int x=100; //health
    const int MAX_ITEMS = 10;
    const int MAX_ITEMS2 =5;
    string inventory[MAX_ITEMS];
    string inventory2 [MAX_ITEMS2];
    int numItems = 0;
    int choice=numItems;
  int numItems2=0;
  char another;

  

 // begin loop
  do
{
    // list of items for input
   
inventory[0] = "sword";
inventory[1] = "armor";
inventory[2] = "shield";
inventory[3] = "knife";
inventory[4] = "gun";
inventory[5] = "axe";
inventory[6] = "hammer";
inventory[7] = "belt";
inventory[8] = "boots";
inventory[9] = "hat";

// list of items for user to pick/see
cout<< "welcome to RPG inventory\n";
cout<<"1. sword\n";
cout<<"2. armor\n";
cout<<"3. shield\n";
cout<<"4. knife\n";
cout<<"5. gun\n";
cout<<"6. axe\n";
cout<<"7. hammer\n";
cout<<"8. belt\n";
cout<<"9. boots\n";
cout<<"10. hat\n\n\n";


// inventory choice and health choice
cout<< "please pick a item to add to your inventory\n";
cout<< "health will drop 10 points for every item\n\n";  
    
   cin>> choice;
  inventory2[numItems2]=inventory[choice-1];
numItems2++;
    cout << "Your items:\n";      
   if (numItems2 < MAX_ITEMS2)
   {
    
    for (int i = 0; i < numItems2; ++i)
    
    
    cout << inventory2[i] << endl;
    
    
    cout << inventory2[numItems2] << endl;
    
    cout<< "your health is now\n";
          x = x - 10; cout << x;
          
          
   }// end if
   else 
   
        
   cout << "You have too many items and can't carry another.";  

// end else


//ending part of loop
  cout << "\n\n\nWould You Like To Play Again? (y/n): ";
cin >> another;

 }while(another == 'Y' || another == 'y') ;// loop
 


 return 0;
}//end main

Dani AI

Generated

A focused checklist and a compact example to bridge the gaps in 's submission and follow 's "one step at a time" advice.

Key fixes to make first

  • Use a single struct to hold a name and its point value (keeps data together and makes score math trivial).
  • Keep a separate score variable (do not reuse health for scoring).
  • Always validate indices before accessing arrays/vectors (the original prints inventory2[numItems2] after incrementing, which can go out of bounds).
  • Check capacity before adding an item and subtract points when removing an item. For a swap, adjust score by the difference: score += new.points - old.points.
  • Implement the menu loop in small, testable pieces (display, add, delete, swap, print).

Minimal C++ example (structure + menu logic)

#include <iostream>
#include <vector>
#include <string>

struct Item { std::string name; int points; };

void printInventory(const std::vector<Item>& inv, int score) {
    std::cout << "Score: " << score << "\n";
    for (size_t i = 0; i < inv.size(); ++i)
        std::cout << i+1 << ". " << inv[i].name << " (" << inv[i].points << ")\n";
}

int main() {
    const size_t MAX_INV = 5;
    std::vector<Item> shop = { {"Sword",10}, {"Armor",20}, {"Shield",8}, {"Knife",4} };
    std::vector<Item> inv;
    int score = 0;
    while (true) {
        printInventory(inv, score);
        std::cout << "1) Add 2) Delete 3) Swap 4) Quit\n";
        int cmd; if (!(std::cin >> cmd)) break;
        if (cmd == 1) { /* list shop, validate choice, push_back and score += points */ }
        else if (cmd == 2) { /* validate inventory index, score -= points, erase */ }
        else if (cmd == 3) { /* validate both indices, score += new.points - old.points, replace */ }
        else break;
    }
}

Troubleshooting and test cases

  • Validate all user input ranges and check for failed reads (non-numeric input).
  • Test edge cases: add when full, delete when empty, swap with same index, invalid menu choice.
  • If an instructor requires fixed arrays instead of vectors, keep an Item inventory[MAX] and an int count that is checked before insert/erase.
  • Build and test one menu action at a time, compile often, and print inventory/score after every change so the effect of each operation is obvious.

Do the requirements just one at a time so that you don't get too confused and overwhelmed by the size of that assignment. Don't try to code it all at one time either -- code a few lines, compile, make corrections, compile again until no more errors. Then code a few more lines.

For example, do this part first:

You will do this using a single structure, so that string names and numeric point values may be housed in a single structure. As the items are used, they are removed from the inventory.

Then move on to the next requirement in the assignment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.