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.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 should print. This inventory should be housed in a single-dimensional string array.
____________________________________________________________________________

#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

Brief diagnosis for : the current program writes past array bounds, reinitializes the master item list each iteration, and prints an element beyond the last valid index. The assignment also requires Add, Delete and Swap actions and printing the inventory after every change — those actions are missing or incomplete. Key fixes are bounds checks, a single counter that tracks the bag size, and moving the master list out of the input loop.

Quick checklist (core fixes)

  • Initialize the master items once, before the main loop.
  • Before adding: check bagCount < BAG_SIZE. Write to bag[bagCount] and then ++bagCount.
  • When printing: iterate from 0 to bagCount-1. Never access bag[bagCount].
  • For delete: shift elements left and decrement bagCount.
  • For swap: validate both indices, then swap the two array elements.
  • Use an input helper that validates numeric input (clear cin on failure).

Example (compact, follows the above rules):

#include <iostream>
#include <string>
#include <limits>
#include <algorithm>

using namespace std;

const int MASTER = 10;
const int BAG = 5;

int readInt() {
  int n;
  while (!(cin >> n)) {
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cout << "Invalid number, try again: ";
  }
  return n;
}

void printMaster(const string items[], int size) {
  cout << "Items:\n";
  for (int i = 0; i < size; ++i) cout << (i+1) << ". " << items[i] << '\n';
}

void printBag(const string bag[], int count) {
  cout << "Inventory (" << count << "):\n";
  for (int i = 0; i < count; ++i) cout << (i+1) << ". " << bag[i] << '\n';
}

int main() {
  string items[MASTER] = {"Sword","Armor","Shield","Knife","Gun","Axe","Hammer","Belt","Boots","Hat"};
  string bag[BAG];
  int bagCount = 0;
  int health = 100;

  while (true) {
    cout << "\nActions: 1-Add 2-Delete 3-Swap 4-Quit\nChoose: ";
    int action = readInt();
    if (action == 1) {
      if (bagCount >= BAG) { cout << "Inventory full.\n"; continue; }
      printMaster(items, MASTER);
      cout << "Pick item number: ";
      int pick = readInt();
      if (pick < 1 || pick > MASTER) { cout << "Bad choice.\n"; continue; }
      bag[bagCount++] = items[pick - 1];
      health -= 10;
      printBag(bag, bagCount);
      cout << "Health: " << health << '\n';
    } else if (action == 2) {
      if (bagCount == 0) { cout << "Inventory empty.\n"; continue; }
      printBag(bag, bagCount);
      cout << "Delete item #: ";
      int del = readInt();
      if (del < 1 || del > bagCount) { cout << "Bad index.\n"; continue; }
      for (int i = del - 1; i < bagCount - 1; ++i) bag[i] = bag[i+1];
      --bagCount;
      printBag(bag, bagCount);
    } else if (action == 3) {
      if (bagCount < 2) { cout << "Need at least two items.\n"; continue; }
      printBag(bag, bagCount);
      cout << "Swap positions: ";
      int a = readInt(); cout << "With: "; int b = readInt();
      if (a < 1 || a > bagCount || b < 1 || b > bagCount) { cout << "Bad indices.\n"; continue; }
      swap(bag[a-1], bag[b-1]);
      printBag(bag, bagCount);
    } else break;
  }
  return 0;
}

Notes and testing: verify invalid input handling (non-numeric), try adding until full to confirm the capacity check, and test delete/swap edge cases. As noted, it helps to state the exact failure; was correct that targeted guidance is better than rewriting everything — the snippet above focuses on the specific bugs visible in the original post.

Recommended Answers

All 3 Replies

What is the question... your code compiles and does everything it's supposed to?

she wants it to do whats in the description

she wants it to do whats in the description

You haven't actually presented us with a problem. You have said it should do this, and not actually pointed out where you are stuck...we don't do it for you we aid you. We are not going to write entire sections of code for you.

Have an attempt to make it do what you asked, if you get stuck then post and tell us what the problem is.

Thanks,
Chris

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.