#include <iostream>
#include <cmath>
usingnamespace 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.
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();
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.
@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.
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
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.
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
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*/)
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.