Deleting part of a structured array?

Yep! Asking about my game again haha. Anyways. There's a part in the game when you're in the battle system, that you can use an item if you'd like (Potions), I have the battle function calling another function, where it does all the work to use an item. The whole code works, but I'm having a slight problem with one thing. It's nothing big, because I can work around it, but if anyone can help me fix it, it'd be even better. So!

What I have the function doing is, it checks to see if the user has any items. If they do, it displays them all, and how many of each item they have.

Ex.
"1. Weak Potion 1"
"1." Is the item number, "Weak Potion" is obviously the name, and "1" is the amount the user currently has.

The function then asks which item the user would like to use, and if they have more than one, how many would they like to use. It runs through that, heals the character, subtracts the amount used and (heres where I'm running into the problem) I have the function checking if the user used all of the item, and if they did, it clears that part of the array.

1
2
3
4
5
6
/*if(inventory[itmuse-1].invamt == 0)
{
inventory[itmuse-1].miscitems = "";
inventory[itmuse-1].invno = "";
inventory[itmuse-1].invamt = NULL;
}*/


That's what I have written, and it's commented out because it breaks my program at this moment. Anyway, after it clears that part, it asks the user if they'd like to use another item. If they say yes, and they used all of the item they had, somehow it's getting rid of all of the users items, because the program displays the message that should only appear when they don't have any items.

Sorry if I'm being unclear, I can post more code if you need it, but let me know what you need to see. The whole programs almost 5000 lines of code so far, so I can't post that on here haha.

Thanks!
Last edited on
I believe inventory is an array of some data-types. Can we see how you declare and define inventory?

E.g
ABC inventory[100];

class ABC {
int invamt;
string miscitems;
string invno;
string invamt;
...
}

We need to take a look at your "class ABC" definition so as to speak.
Sure, here it is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct Character{
	string name;
	string weapon;
	string armor;
	string Class;
	string race;
	string miscitems;
	string currentweapon;
	string currentarmor;
	int Health;
	int invamt;
	int BaseAttk;
	int MaxHealth;
	int ArmorClass;
	int Exp;
	int clevel;
	int MaxMp;
	int Mp;
	string invno;
	string progress;
	int gold;
} inventory[28];
Any ideas?
inventory[itmuse-1].invamt = NULL;
Is invamt a pointer?

Also
1
2
3
4
5
6
7
if(inventory[itmuse-1].invamt == 0)
{
  inventory[itmuse-1].miscitems = "";
  inventory[itmuse-1].invno = "";
  inventory[itmuse-1].invamt = NULL; //we already know invamt == NULL 
    // because of the conditional statement on line 1? (NULL == 0)
}
Last edited on
No, it's not. And oh wow. I didn't even realize that. I'll go change the code and see if that might be the problem.

EDIT: Nope, that wasn't the problem.
Last edited on
Sorry it didn't help.

BTW, we use NULL to set pointers to 0, meaning they point to nothing (an invalid memory address)...
Remember that objects in c++ are not references, we use pointers for that.
How is setting the items state to 0 and/or "" removing it from your inventory?
Use a vector (probably of pairs) so you don't have to have "Null Objects" floating around.
It's erasing its name and inventory number, and the items should only display if it does have something in it.

Ex.
1
2
3
4
if(inventory[itmuse-1].miscitems!="")
{
cout << "message" << endl;
}
Couldn't you do something like:

1
2
std::vector< std::pair<object, int> > inventory;
//object is the object, int is how many you have 
Well you could have but I don't know if you want to edit 5000 lines of code though... Haha!
Is it still not working?
Nope, so I'll just have it display 0 left, since it basically means the same thing. I just thought it might be a little less confusing if it just disappeared altogether if there was none left.
you could always have a check if the number of items left is equal to 0, and only output if that is not true.
just a thought.
Topic archived. No new replies allowed.