Utilizing a vector for an inventory program?


This is a small section of code I have below, which is basically how to create an inventory program for beginners, which I am.

In this programs case, I think, the vector would be used to keep track of the amount of currency/gold a user has and then after the user is finished the elements of the inventory would be displayed.

From what I have gathered the functions necessary for the utilization of a vector in a program like this would be push_back.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<vector>

using namespace std;


double goldAmount = 25;
int selection;
vector<string>inventory;

void potion();
void sword();
void tent_1();

void potion(){
	
	int potion;
	
	if (goldAmount < 2.5){
		
		cout<<"\nYou Do Not Have Enough Gold!"<<endl;
		return;
	}
	potion++;
	goldAmount = goldAmount - 2.5;
	cout<<"\nYou Purchased A Potion."<<endl;
}

void sword (){

       int sword;
       
       if (goldAmount < 10){

              cout<<"\nYou Do Not Have Enough Gold!"<<endl;
		return;
       }
	sword++;
	goldAmount = goldAmount - 10;
	cout<<"\nYou Purchased A Sword."<<endl;
}

void tent_1(){
	
	int tent_1;
	
	if (goldAmount < 5){
		
		cout<<"\nYou Do Not Have Enough Gold!"<<endl;
		return;
	}
	
	tent_1++;
	goldAmount = goldAmount - 5;
	cout<<"\nYou Purchased A Tent for one person."<<endl;
}

int main(){
	
	do{
	cout<<"\nPurchase Items for your Inventory"<<endl;
	cout<<"***********************************"<<endl;
	cout<<"1. Potion - 5g"<<endl;
	cout<<"2. Sword - 10g"<<endl;
	cout<<"3. Tent (1 Person) - 5g"<<endl;
        cout<<"4. Exit"<<endl;
	cout<<"You Have "<<goldAmount<<" gold"<<endl;
	cout<<"Please Make a Purchase: ";
	cin>>selection;

        switch(selection){
		case 1: potion();
		break;
		case 2: sword();
		break;
		case 3: tent_1();
		break;
}

}while(selection != 13);
}


So, my question is how, would I utilize a vector as described above?
Last edited on
What is your question?
How would I utilize a vector within an inventory? As described above?
what is the vector for? Each customer?
some rough ideas..
add a 'is deleted' bool to the data structure. then you can 'delete' items without the mess on the fly and clean up a bunch of them at once every now and then.
add an 'is dirty' type field as well. (this is above the vector, not inside it)
use push back to add a new one. any additions triggers is-dirty.
each entry needs a key.
sort the vector by the key before doing any binary search on the keys to find an entity, if is-dirty. If not, its still sorted from last time.

from time to time sort off is-deleted, remove the deleted ones off the back end using vector's tools
resort on key.

if resorting all the time is a mess you can track which portion is sorted and binary search that part and linear search the recent additions until you get enough additions to justify the sort.

if you want to get extra fancy you can do stuff like custom comparison for the sort that throws the deleted to the end, track where those start, and overwrite them (instead of push-back) with new additions. This is more complex but it eliminates the need to remove the deleted stuff.

if its not obvious yet, you need this format for the things I am saying:
class external
{
bool is_dirty;
other stuff;
vector<inner_datastore_class> v;
external my_push_back(inner_datastore_class & x) {v.push_ back(x); v.sort(..); etc;}
};

the external wrapper may look like the vector -- it won't do much more than provide access to the vector, but it will have just a little extra logic and a couple of extra variables to help do the things you may want to do.
Last edited on
The vector I believe is for keeping track of the amount of gold that the user has, and soon being used to display the elements of the inventory.
not going to chase down links but amount of gold does not need a vector. The only use I see of having a vector is to keep a history of their money as they earn more and buy stuff etc. You could use it for that... is the assignment just to add a vector for the sake of adding a vector? Or is it the inventory, so you can see that you have 10 tents and 3 swords or whatever?

If its the inventory, you have 2 approaches.
1) make a vector of integers (init to zero), 1 for each product. looks like
enum {tent, sword, potion, emax} ; vector<int> inventory(emax); inventory[tent]++; //bought a tent.
2) a vector of the thingys, so you have {tent, potion, potion, tent, sword, ...} for all the purchases.

I am a big fan of approach 1 for small problems like this.
Last edited on
Some game related store/inventory source I ran across a while back using custom classes/structs and vectors (pretty hackish and ugly, but it works):

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

struct Item // used to store a single item from the store.
{
   std::string name;
   int         price;
};

class Store // used to handle everything to do with the store
{
public:
    Store(const std::vector<Item>& itemList, int n);
   ~Store() {}

public:
   std::string BuyItem(int item);
   std::string viewInventory();
   std::string ListItems();
   int         getMoney()
   {
      return std::max(money, 0);
   }

private:
   std::vector<Item> inventory;
   std::vector<Item> forSale;
   int               money;
};


int main()
{
   std::vector<Item> f(3);
   f[0].name  = "Clown";
   f[0].price = 2;
   f[1].name  = "Cracker Jack";
   f[1].price = 6;
   f[2].name  = "Camel";
   f[2].price = 9;

   Store s = Store(f, 3);

   bool quit = false;
   int  input;

   while (!quit)
   {
      do
      {
         std::cout << "Welcome to the store.\n"
            << "You have " << s.getMoney()
            << " dollars.\n"
            << "\nWhat would you like to do?\n"
            << s.ListItems()
            << "[4]View your inventory\n"
            << "[5]Leave\n";
         std::cin >> input;
      } while (input < 1 || input > 5);

      switch (input)
      {
      case 4:
         std::cout << s.viewInventory();
         break;

      case 5:
         quit = true;
         break;

      default:
         std::cout << s.BuyItem(input);
      }
   }

   std::cout << "\nSee ya!\n";
}


Store::Store(const std::vector<Item>& itemList, int n)
{
   for (int i = 0; i < n; i++)
   {
      forSale.push_back(itemList[i]);
   }
   money = 20;
}

std::string Store::BuyItem(int item)
{
   money -= forSale[item - 1].price;

   if (money < 0)
   {
      return "\nSorry, you don't have enough money.\n\n";
   }

   inventory.push_back(forSale[item - 1]);

   return "You bought a " + forSale[item - 1].name + '\n';
}

std::string Store::ListItems()
{
   std::string s;

   for (unsigned int i = 0; i < forSale.size(); i++)
   {
      s += "[";
      s += i + 49;
      s += "] Buy a ";
      s += forSale[i].name;
      s += " ($";
      s += forSale[i].price + 48;
      s += ")\n";
   }
   return s;
}

std::string Store::viewInventory()
{
   std::string s = "\n";

   for (unsigned int i = 0; i < inventory.size(); i++)
   {
      s += inventory[i].name + '\n';
   }

   return s + '\n';
}
Welcome to the store.
You have 20 dollars.

What would you like to do?
[1] Buy a Clown ($2)
[2] Buy a Cracker Jack ($6)
[3] Buy a Camel ($9)
[4]View your inventory
[5]Leave
3
You bought a Camel
Welcome to the store.
You have 11 dollars.

What would you like to do?
[1] Buy a Clown ($2)
[2] Buy a Cracker Jack ($6)
[3] Buy a Camel ($9)
[4]View your inventory
[5]Leave
2
You bought a Cracker Jack
Welcome to the store.
You have 5 dollars.

What would you like to do?
[1] Buy a Clown ($2)
[2] Buy a Cracker Jack ($6)
[3] Buy a Camel ($9)
[4]View your inventory
[5]Leave
1
You bought a Clown
Welcome to the store.
You have 3 dollars.

What would you like to do?
[1] Buy a Clown ($2)
[2] Buy a Cracker Jack ($6)
[3] Buy a Camel ($9)
[4]View your inventory
[5]Leave
1
You bought a Clown
Welcome to the store.
You have 1 dollars.

What would you like to do?
[1] Buy a Clown ($2)
[2] Buy a Cracker Jack ($6)
[3] Buy a Camel ($9)
[4]View your inventory
[5]Leave
4

Camel
Cracker Jack
Clown
Clown

Welcome to the store.
You have 1 dollars.

What would you like to do?
[1] Buy a Clown ($2)
[2] Buy a Cracker Jack ($6)
[3] Buy a Camel ($9)
[4]View your inventory
[5]Leave
5

See ya!
Topic archived. No new replies allowed.