Using Vectors for a Clip/Cache

I've made it pretty far with a program but ran into a few problems. I'm trying to make it like a game. You would fire your gun, then have the option of reloading. If you run out of ammo and try to fire...it will automatically come out of your cache. Anyone who played a 3rd or first person shooter knows what I mean. I thought vectors would be the best course of actions since they can remove and add elements with ease. One of the many problems I have is subtracting the Hand Guns current ammo (size) from its maximum (capacity) to see how much to A. push_back into the clip and B. pop_back out of the cache. Can size() and capacity even be subtracted? Here's the code with what I believe to be all the possibilities.

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
#include<iostream>
#include<vector>

using namespace std;

int main(int argc,char** argv)
{
	vector<int> HG_cache (36,1);
	vector<int> HG_clip (12,1);
	char user_input;

	cout << "Press r to fire or o to reload." << endl;

	//Made an inescapable loop.
	for(;;)
	{
		//Accept user input.
		cin >> user_input;

		//Fire gun and have ammo.
		if(user_input='r' && HG_clip.size()!=0)
		{
			cout << "Bang!" << endl;
			HG_clip.pop_back();
		}

		//Try to fire gun but out of ammo in clip.
                Main problem. How do I push back clip back to twelve depending                   on its current size.
		if(user_input = 'r' && HG_clip.size()=0 && HG_cache.size()>0)
		{
			cout << "Reloading" << endl;
			HG_clip.push_back();
			HG_cache.pop_back()=HG_clip.size() - HG_clip.capacity();
		}

		//Try to fire gun but no ammo in cache.
		if(user_input = 'r' && HG_clip.size()=0 && HG_cache = 0)
		{
			cout << "Click" << endl;
		}

		//Try to reload but clip is full.
		if(user_input = 'o' && HG_clip.size() = HG_clip.capacity())
		{
			continue;
		}

		//Try to reload but nothing in cache.
		if(user_input = 'o' && HG_clip.capacity() = 0)
		{
			continue;
		}

		//Succeeds in reloading.
		if(user_input = 'o' && HG_clip.size() < HG_clip.capacity())
		{
                cout << "Reloading" << endl;
		HG_clip.push_back();
		HG_cache.pop_back() = HG_clip.size() - HG_clip.capacity();
		}
	}

		return 0;
}
It doesn't look like you care about the elements stored in the vectors. All you care about is how many they are. You can use simple int variables for that.
Last edited on
Topic archived. No new replies allowed.