I want to make a dynamic menu in which it detects whether you have one or more durability in your inventory. If you do have one or more, it prints out into the menu. Otherwise, it won't.
Here's the code:
#include <iostream>
#include <vector>
using namespace std;
class A {
protected:
int durability = 3;
public:
virtual void attack() { };
virtual int usage() { return 1; };
virtual string weaponName() { return "Sword x"; };
};
class B : public A
{
public:
void attack() { durability--; cout << "B Attack" << endl; cout << durability; };
string weaponName() { return "Sword B"; };
};
class C : public A
{
public:
int usage() { return durability; };
void attack() { durability--; cout << "C Attack" << endl;cout << durability; };
string weaponName() { return "Sword C"; };
};
class D : public A
{
public:
void attack() { durability--; cout << "D Attack" << endl;cout << durability; };
string weaponName() { return "Sword D"; };
};
int main(void)
{
B * b = new B;
C * c = new C;
D * d = new D;
int k = 10;
vector <A*> tableOfAs;
tableOfAs.push_back(b);
tableOfAs.push_back(c);
tableOfAs.push_back(d);
while (--k>0)
{
int i = 0;
vector <A*> options;
for (i = 0; i < tableOfAs.size(); i++)
{
if (tableOfAs[i]->usage() > 0){
options.push_back(tableOfAs[i]);
} else { delete tableOfAs[i]; }
}
if (options.size() == 0)
break;
cout << "Attack Options:" << endl;
for (i = 0; i < options.size(); i++)
cout << i << ". " << options[i]->weaponName().c_str() << endl;
int choise;
cin >> choise;
if (choise<0 || choise > options.size()-1)
cout << "Wrong option" << endl;
else
options[choise]->attack();
}
return 1;
}
My problem here is that the durability gets zeroed and gets deleted and then after placing another choice, the console crashes.