Adding elements to an array?

Hi. I'm trying to make a sort of text "shop". Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <cmath>
using namespace std;
void Shop(int);

int Gold = 10;

int HealthPotion = 0;
int ManaPotion = 0;

int Choice;
string Inventory[5];


int main(){



cout << "You are in the shop. What would you like to buy?";
cout << " You have " << Gold << " gold.";
cout << endl;
cout << "1\) Health potion: 2 gold.  2\) Mana Potion: 3 gold.";
cin >> Choice;
Shop(Choice);

cin.ignore();
cin.get();
return 0;
}

void Shop(int x){
 if(x==1){
   HealthPotion++;
   Gold = Gold-2;
   }
 if(x==2){
   ManaPotion++;
   Gold = Gold-3;
   }

}


All that works just fine. What I want to do is if they buy something, it adds it to the next available element in the array "Inventory". Basically if I buy a Health Potion and my inventory is empty, it becomes Inventory[0].

I have NO idea how to do this and the google searches I've done have come up empty or with over-complicated information.

Can someone please explain to me how to do this?
The number of elements in an array is always fixed, you cannot change it.
That's what vectors are for:
http://www.cplusplus.com/reference/stl/vector/
Probably the easiest way to do this is to use an std::vector.

Here are some side-by-side comparisons of arrays and std::vectors:

1
2
3
4
5
6
7
8
9
//Constant-size array
//Initialize
type name[size];
//Access an element
name[position];
//Resize
//Impossible if you use the name[size] syntax.
//Get the size:
//It's best to store it in a variable. 


1
2
3
4
5
6
7
8
9
//Vector
//Initialize
std::vector<type> name(size);
//Access an element
name[position];
//Resize
name.resize(new_size);
//Get the size:
name.size();


EDIT: Samurai'd.

-Albatross
Last edited on
If you want each purchase to be assigned to the first empty element of the array, you need to have a variable that keeps track of the current number of items already in the inventory int itemsInInventory;. That way, when you buy an item, you can add the line ++itemsInInventory; and when you remove an item,
--itemsInInventory.
Last edited on
closed account (z0RDjE8b)
It's easy just do this:

1
2
3
4
5
string Caga[3];
Caga[0] = "S";
Caga[1] = "H";
Caga[2] = "O";
Caga[3] = "P";
Last edited on
Actually it would be:

1
2
3
4
5
6
 
char Caga[4];
Caga[0] = 'S';
Caga[1] = 'H';
Caga[2] = 'O';
Caga[3] = 'P';


Your code would only hold SHO and is using an overkill data type as string Caga[3] would make it so you could do:

1
2
3
4
string Caga[3]; 
Caga[0] = "SHOP";
Caga[1] = "SHOP";
Caga[2] = "SHOP";
@BHXSpecter: If you stop feeding it, it will die of starvation
@atropos: Just didn't want TheJabre to believe BooleanBoy's wrong code. Though, I stopped looking at the new user posts as I've grown tired of the trolling. I assumed Jabre was a real new user and was just warning on the bad code.

Not meaning to feed the troll.
Ah, I never thought of that possibility. I just assumed that you replied to his/her/its/their post because you took the bait, so to speak; now I see that it was done to correct his/her/its/their subscript range error. Sorry I jumped the gun like that.

He/she/it/they have some people on edge and/or exasperated ATM.
like me
This dinky little program screams Object Oriented to me.

Customer class has an Inventory class and boom! Your code will be so sexy. lol

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Inventory
{
	public:
	bool AddItem(int item)
	{
		bool purchased = true;
		switch (item)
		{
			case 0: inventory.push_back('H');
				break;
			case 1: inventory.push_back('M');
				break;
			default:
				purchased = false;
		}
		return purchased;
	}

	void Write(ostream& out) const
	{
		for (int i = 0; i < inventory.size(); i++)
			out << inventory[i] << " "; 
	}
	private:
	vector<char> inventory;
};

int main() 
{
	
	Inventory myInventory;

	myInventory.AddItem(0);
	myInventory.AddItem(1);
		
	myInventory.Write(cout);

	return 0;

};


I would actually use enums and valid a bit more but this is how I would approach it.

TS, look into Object Oriented Programming because it's awesome and will save you the stress of procedural programming. It's not the be all-end all of programming but it helps in most situations where you want more readable code and instead of absolute performance.
Last edited on
@atropos It's okay, though for fun I ran BooleanBoy's code but changed string to char:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
	char Cage[3];
	Cage[0] = 'S';
	Cage[1] = 'H';
	Cage[2] = 'O';
	Cage[3] = 'P';
	
	for (int i = 0; i < 4; i++)
	{
		cout << Cage[i] << ' ';
	}
	cout << endl;
	return 0;
}

And got this output:

S H O ♥

Don't know what everyone will see but in my test it printed SHO and then a heart. Soon as I saw it I thought "The app is showing SHO Love (Sure love)." lol
Last edited on by closed account z6A9GNh0
Because:
char Cage[3];
only goes from Cage[0] to Cage[2].

If you write to anything beyond that, you may get...unexpected results.
I know, just love the 'unexpected results' being a heart.
I ♥ how none of us actually gave the TS a straight answer to his question. I just can't stomach explaining array like that. It's the reason why I can never be a teacher, even though both of my parents were. haha
vectors were suggested otherwise you can do a dynamically allocated array or use pointers to change the allocated memory sections which the array covers, extending it size,

myvector.push_back(/*puts this at end of container*/)

that is the easiest way to do it.
I ♥ how none of us actually gave the TS a straight answer to his question. I just can't stomach explaining array like that. It's the reason why I can never be a teacher, even though both of my parents were. haha

Albatross gave them a straight answer. Vectors is the best way to do it with push_back() as ui-uiho just pointed out.
I think TS wanted to store an item in the first empty element of the array, not dynamically resize the array.
TheJabre wrote:
What I want to do is if they buy something, it adds it to the next available element in the array "Inventory". Basically if I buy a Health Potion and my inventory is empty, it becomes Inventory[0].


..which I interpreted as: If I buy something, and the inventory is empty, the thing I bought is stored in the first element of the array. If I buy something and already have two items in Inventory, the thing I bought is stored in the third element of the array.

That leads me to think the TS needs a variable to keep track of the current first empty element of the array, and use this variable as the array subscript to store new items or remove them.
Okay. Confusion confusion lol.

Someone mentioned vectors and I researched that in the C++ books I have and bam, I have a working script. Thanks to everyone who tried to help!
Topic archived. No new replies allowed.