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;
cin >> selection;
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;
}
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". Obviously, to turn this into a "real" inventory system, it'll need much more tweaking- this excercise is to help me learn how to search vectors... next will be removing the item once it is used (really looking forward to that, too... )
Any advice you can offer would be awesome. Thanks for slogging through all this!
`selection' is set to 0 at the beginning of the code, and then you have a `switch(selection)'. That doesn't seem right... `selection' can only possibly 0 at that point.
`selection' is set to 0 at the beginning of the code, and then you have a `switch(selection)'. That doesn't seem right... `selection' can only possibly 0 at that point.
Sorry, missed a line! see edit- "cin >> selection;"
Right nevermind I have my python hat on today and not my C++ hat
The problem you are having is that the iterator returned by inventory.at(choice) not necessarily needs to be inventory.end(), if you change the end into an inventory.at(choice) it should work. Be careful though as choice can index outside of your inventory vector as maximum choice is defined as inventory.size() + 1, see the output loop above in which you create the choice menu.
I don't really understand why you are trying to use "find" in there, the best approach would be adding a itemType on the "aItem" struct/class and look directly at the choice - 1 (don't forget to check if choice is > 1 and <= inventory.size()) to check if itemType == SOME_POTION_IDENTIFIER.
If you really want to use the find, the first thing that I see that I believe is wrong is that the search range of find doesn't include de second argument, and this second argument is returned in case of a search failure. So you should replace the != inventory.end() by inventory.at(choice).
Also, I do not think find will be able to compare the value the way you are passing to it.
How is the aItem struct/class implemented?
The implementation is mostly to teach myself vectors. At the end of it all, I want a simple program that will add an item to a vector, show a list of items, allow the player to pick one, execute the correct code for the item selected (not simply by looking at the choice, as these items are consumable- choice 3 won't always be a healing potion, for example), and then delete the item after it is used. I am attempting a larger project, but didn’t want my difficulties with this bit of code muddling up with difficulties from the rest of the code! So I have this running as a self-contained solution. Once I’m comfortable with managing vectors, I’ll design a couple more “simulations”, and then hold my breath and pray as I turn it into actual game code and merge it with my tiny project!
I realize that I'm probably making a mountain range out of a molehill... but I'm trying to learn as I go!
Well, as I said, comparing by name is a bad idea, it is slow and prone to error. If you using char* of char[], they won't compare as you may want. For instance:
[source lang="cpp"] char* a = "hello world";
char b[] = "hello world";
if (a == b){
printf("Ok");
}
else{
printf("Nope.");
}[/source]
Will print "Nope.", which is not intuitive at all (it has to do with memory areas used by a and b).
Using find is also not needed as you know which item you need to check, even if you get it working (I believe that if you use Strings, aItem is a struct and they are the first element declared in the struct it will work as intended), you will be tossing away processing (as you need to find the element in the array).
I keep my opinion of adding an itemType, each type is an ID that represents the item type (armor, potion, quest item and so on). So you just check if the item at that point have the potion id. The main advantage of a vector over a list is that you can directly accesss an item... take advantage of it =D
...The main advantage of a vector over a list is that you can directly accesss an item... take advantage of it =D
Oh, my God. That made it so much easier. I ended up with:
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;
cin >> selection;
int a = 1;
switch (selection)
{
case 1:
cout << "Which item would you like to use?" << endl;
for( vector<aItem>::size_type index = 0; index < inventory.size(); index++ )
{
cin >> choice;
//This checks for an invalid response. Add in non-int
if ((choice - 1) >= inventory.size())
{
cout << "Choice out of bounds. Stop being a dick." << endl;
}
else
{
if(inventory[choice - 1].itemType == 01)
{
cout << "You used a healing potion!" << endl;
}
else
{
cout << "Invalid Item type" << endl;
}
}
break;
case 2:
cout << "Why do you have to be so difficult? Pick 1!" << endl;
break;
This code compiles and runs- but is it good code, in your opinion?
My next task is to figure out how to delete the item from the vector and move everything up to fill the spot. That'll come later, I am still basking in the delight of this working. Thanks!
The code is artificial, and more importantly, it doesn't do anything (except printing stuff). Thus there's very little that could be wrong with it on low level, and in the absence of context it's hard to say anything about design issues.
Just some cosmetic things I'd point out to a student:
- "a" is useless because you could be printing "index+1" instead
- any variables like "choice" should be defined as close as possible to where you actually use them for the first time
- instead of writing "choice-1" everywhere, sanitize the number once to the value you actually want (-=1)
- finally, magic numbers like "01" and "02" are generally bad; in a real program you would likely use an enum and descriptive labels like ITEM_HEALTHPOT or ITEM_FIREPOT
Which compiler are you using? If it's new enough to have some C++11 features, they can make your life easier in small (and occasionally big) ways.