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