it suppose to take the user input(a number) and put that item into your item into the inventory(1. hat, puts hat into the inventory). After that I need a loop that will keep adding items into the inventory intill it reaches the max and allow the user to discard one/replace. At the same time, every time the user chooses a item, their health goes down by 10 intill it goes down to 20 and then the person finds a healing potion that restore 100.

I'm having probelms making it take in take the user input and putting that item into the inventory. I have tried to put it into a loop but it doesn't keep the past items. for the health, what would you do? Make a if/else statement..? I'm lost, please help


here what I got

is there a way to print out the list of item to print without cout it?

// Hero's Inventory
// Demonstrates arrays

#include <iostream>
#include <string>

using namespace std;

int main()
{
   
   
    const int MAX_ITEMS = 10;
    string inventory[MAX_ITEMS];
    string inventory2 [MAX_ITEMS];
    int numItems = 0;
    int choice=numItems;
  int numItems2=0;
  
  //health

  int x=100;
  
    // list of items for input
    inventory[numItems] = "sword";
    inventory[numItems] = "armor";
    inventory[numItems] = "shield";
    inventory[numItems] = "knife";
    inventory[numItems] = "gun";
    inventory[numItems] = "axe";
    inventory[numItems] = "hammer";
    inventory[numItems] = "armor";
    inventory[numItems] = "boots";
    inventory[numItems] = "hat";


// list of items for user to pick
cout<< "welcome to RPG inventory2\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. armor\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--];
          
   if (numItems2 < MAX_ITEMS)
   {
    
    for (int i = 0; i < numItems2; ++i)
        cout << inventory2 << endl;
    cout << "Your items:\n";
    
    cout << inventory2[numItems2] << endl;
    
    cout<< "your health is now\n";
          cout<<  x-10<<endl;
          
   }
   else
   cout << "You have too many items and can't carry another.";
    cout << "\nYour items:\n";
  



 
 
int blank;
    cin >> blank;

 return 0;
}

Recommended Answers

All 4 Replies

> inventory[numItems] = "sword";
> inventory[numItems] = "armor";
> inventory[numItems] = "shield";
All of these overwrite one another.
Try

inventory[0] = "sword";
    inventory[1] = "armor";
    inventory[2] = "shield";

> cout<<"1. sword\n";
> cout<<"2. armor\n";
Rather than relisting the text string, use a loop and output strings direct from the inventory array you just set up.

> inventory2[numItems2++]=inventory[choice--];
You've out-witted yourself with the over use of ++ and --
Expand it out to

inventory2[numItems2]=inventory[choice-1];
numItems2++;

> cout << inventory2 << endl;
You mean cout << inventory2[i] << endl;

// Hero's Inventory
// Demonstrates arrays

#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";
          cout<<  x-10<<endl;
          
          
   }// 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

how would I get x to continue going down by 10? I tried a number of things but nothing is working. I'm having to hardest time getting else staments to work out so when it goes down to 20, I can push the health up to 100 again.

any advice on how to replace a item with another when I get to max inventory

> cout<< x-10<<endl; x = x - 10; cout << x; Again, it's trying to do too much in as few characters as possible which is tripping you up.

well now, I can't believe I overlooked that; I was thinking that it had to have somthing to do with the loop that I wasn't paying attention to somthing that small, thanks

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.