inventory program

ok i am trying to make an inventory program with a max amount of spells, weapons, armor, and other items of 10. i want them all to be in the inventory and i want them to have a stick in weapons (for a little while until they buy something better), a fire spell in spells, an old robe in armor, and 2 different potions in items. but im not sure what to do exactly. i think that this is bout right but my compiler gives me errors. like

no match for 'operator[]' in maxSpells[spells]




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
     /*inventory*/
     string spells;
     string weapons;
     string armor;
     string items;
     const int maxSpells = 10;
     const int maxWeapons = 10;
     const int maxArmor = 10;
     const int maxItems = 10;
     string inventory[maxSpells];
     inventory[maxWeapons];
     inventory[maxArmor];
     inventory[maxItems];
     maxSpells[spells] = "fire";
     maxArmor[armor] = "old robes";
     maxWeapons[weapons] = "stick";
     maxItems[items] = "health potion" "magic potion";

     /*inventory*/



If you're creating an array of spells it's like this:
1
2
3
const int maxSpells = 10;
string spells[maxSpells];
spells[0] = "fire"//etc.. 


Is 'inventory' a struct or class?
Last edited on
its not a class but i dont know what a struct is so ima have to go with struct
i dont know what a struct is so ima have to go with struct

Huh? Well, a good way to start learning (data) structures is of course here:
http://www.cplusplus.com/doc/tutorial/structures/
Answering your original question:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct inventory
{
       string spell;
       string weapon;
       //etc...
       string item1;
       string item2;
}

//somewhere else we'll create an instance of the struct and initialize its members
inventory hero;
hero.spell = "firaga";
hero.weapon = "broadsword";
hero.item1 = "wallet";
hero.item2 = "leather belt";
//to display it...
std::cout << "Our hero's weapon is " << hero.weapon << std::endl;//etc.. 

not the most efficient, but i hope it gives a clearer picture
Last edited on
1
2
3
inventory[maxWeapons];
inventory[maxArmor];
inventory[maxItems];


These lines have no effect whatsever; you are dereferencing the array called inventory, but not doing anything with it. Basically, reading a value from the array, and then just moving on, making no changes and doing nothing with the value you read (which is actually off the end of the array anyway, and would possibly cause a segfault at runtime).


1
2
3
const int maxSpells = 10;
...
maxSpells[spells] = "fire";


maxSpells is an integer; the number 10. You're then trying to treat it like an array, and put the value "fire" into an element you're specifying with an uninitialised string instead of a number. This is nonsensical. The same for lines 15,16,17.

I think you need to step back and make sure you understand what an array is, and how to create and use them.
Last edited on
ok thanx yall but i have just one more question...

what does the following error mean:
error: no match for 'operator[]' in .........
no match for 'operator[]' in maxSpells[spells]

It's just your compiler telling you it doesn't know what to do with the instruction "const int [string];", remember that the normal behavior of "[]" is to let the compiler know that:
i. you're declaring an array, e.g char name[] = "cainen172";
or,
ii. you're specifying an array elements e.g
string spells[5];//declare five variables named spells that can hold string values

also Moschops has already explain it:
maxSpells is an integer; the number 10. You're then trying to treat it like an array, and put the value "fire" into an element you're specifying with an uninitialised string instead of a number.


ooooohhhh i gets it now. THANX EVERYONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :)
Topic archived. No new replies allowed.