Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 库存的动态菜单_C++ - Fatal编程技术网

C++ 库存的动态菜单

C++ 库存的动态菜单,c++,C++,我想做一个动态菜单,它可以检测你的库存中是否有一个或多个耐用性。如果您有一个或多个,它会打印到菜单中。否则,它不会 代码如下: #include <iostream> #include <vector> using namespace std; class A { protected: int durability = 3; public: virtual void attack() { }; virtual int usage() { retur

我想做一个动态菜单,它可以检测你的库存中是否有一个或多个耐用性。如果您有一个或多个,它会打印到菜单中。否则,它不会

代码如下:

#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;
}
#包括
#包括
使用名称空间std;
甲级{
受保护的:
int耐久性=3;
公众:
虚空攻击(){};
虚拟int用法(){return 1;};
虚拟字符串weaponName(){返回“剑x”;};
};
B类:公共A
{
公众:
void attack(){耐久性--;cout尝试另一种方法
创建一个具有一些纯虚拟功能的父类武器

  • 武器名称()

  • 用法()

  • 攻击()

然后创建从武器类继承这些的类,并相应地实现

使用usage方法进行检查,如果结果大于0,则将其添加到类的指针表中。 然后在选项列表中使用weaponName函数打印名称,然后在选择时使用表索引中对象的attack()方法。 因此,如果棍棒剑在表的索引1中,您调用
weaponInv[1]。attack()
它将调用棍棒剑攻击

以下是建议逻辑的简单演示:

更新:
#包括
#包括
甲级{
公众:
虚空攻击(){};
虚拟int用法(){return 1;};
虚拟std::string weaponName(){返回“剑x”;};
};
B类:公共A
{
公众:
void attack(){std::cout尝试另一种方法
创建一个具有一些纯虚拟功能的父类武器

  • 武器名称()

  • 用法()

  • 攻击()

然后创建从武器类继承这些的类,并相应地实现

使用usage方法进行检查,如果结果大于0,则将其添加到类的指针表中。 然后在选项列表中使用weaponName函数打印名称,然后在选择时使用表索引中对象的attack()方法。 因此,如果棍棒剑在表的索引1中,您调用
weaponInv[1]。attack()
它将调用棍棒剑攻击

以下是建议逻辑的简单演示:

更新:
#包括
#包括
甲级{
公众:
虚空攻击(){};
虚拟int用法(){return 1;};
虚拟std::string weaponName(){返回“剑x”;};
};
B类:公共A
{
公众:

void attack(){std::cout回顾这篇旧文章。我修改了boring32中的代码。我使编程新手更容易阅读

#include <iostream>
#include <string>
#include <vector>
using namespace std;
class baseClass {
public:
    virtual void attack() {};
    virtual int usage() { return 1; }
    virtual string weaponName() { return "Sword x"; }
};
class Sword : public baseClass
{
private:
    int durability;
    string name;
public:
    /*Sets the name and the durability of the sword*/
    Sword(string nameToSet, int dur) :name(nameToSet),durability(dur) {}
    int usage() { return durability; }
    /*Decreases durability by one and tells the name of the sword used in the attack*/
    void attack() { 
        durability--; 
        cout << name <<" Sword Attack" << endl;
    }
    /*returns weapon name*/
    string weaponName() { return name +" Sword"; }
};
int main()
{
    Sword sworda("One", 1), swordb("Two", 2), swordc("Three", 3);
    int loop = 10;
    int choice;
    /*Add swords into a holding container first*/
    vector <baseClass*> holdingContainer;
    holdingContainer.push_back(&sworda);
    holdingContainer.push_back(&swordb);
    holdingContainer.push_back(&swordc);

    while (--loop>0)
    {
        /*Decide whether swords in the holding container has a durability of one or more
          before adding them into the menu*/
        vector <baseClass*> swordWithDurabilityContainer;
        for (int i = 0; i < holdingContainer.size(); i++)
        {
            if (holdingContainer[i]->usage() > 0) {
                swordWithDurabilityContainer.push_back(holdingContainer[i]);
            }
        }

        /*Check if there's any items in the swordWithDurabilityContainer otherwise break out of the loop*/
        if (swordWithDurabilityContainer.size() == 0) { break; }

        /*Print the items*/
        cout << "Attack Options:" << endl;
        for (int i = 0; i < swordWithDurabilityContainer.size(); i++) {
            cout << i << ". " << swordWithDurabilityContainer[i]->weaponName().c_str() << endl;
        }
        /*Ask for user input*/
        cin >> choice;

        /*Check if the input is valid*/
        if (choice<0 || choice > swordWithDurabilityContainer.size() - 1) {
            cout << "Wrong option" << endl;
        }
        else {
            swordWithDurabilityContainer[choice]->attack();
        }
    }
    /*Notify the user that the loop has ended*/
    cout << "No more items in the list(swordWithDurabilityContainer variable)";
}
#包括
#包括
#包括
使用名称空间std;
类基类{
公众:
虚空攻击(){};
虚拟int用法(){return 1;}
虚拟字符串weaponName(){返回“剑x”;}
};
类剑:公共基类
{
私人:
耐用性;
字符串名;
公众:
/*设定剑的名称和耐久性*/
剑(字符串nameToSet,int dur):名称(nameToSet),耐久性(dur){}
int usage(){返回耐久性;}
/*耐久性降低1,并告知攻击中使用的剑的名称*/
无效攻击(){
耐久性--;

cout回顾这篇旧文章。我修改了boring32中的代码。对于那些新编程的人来说,我使它更容易阅读

#include <iostream>
#include <string>
#include <vector>
using namespace std;
class baseClass {
public:
    virtual void attack() {};
    virtual int usage() { return 1; }
    virtual string weaponName() { return "Sword x"; }
};
class Sword : public baseClass
{
private:
    int durability;
    string name;
public:
    /*Sets the name and the durability of the sword*/
    Sword(string nameToSet, int dur) :name(nameToSet),durability(dur) {}
    int usage() { return durability; }
    /*Decreases durability by one and tells the name of the sword used in the attack*/
    void attack() { 
        durability--; 
        cout << name <<" Sword Attack" << endl;
    }
    /*returns weapon name*/
    string weaponName() { return name +" Sword"; }
};
int main()
{
    Sword sworda("One", 1), swordb("Two", 2), swordc("Three", 3);
    int loop = 10;
    int choice;
    /*Add swords into a holding container first*/
    vector <baseClass*> holdingContainer;
    holdingContainer.push_back(&sworda);
    holdingContainer.push_back(&swordb);
    holdingContainer.push_back(&swordc);

    while (--loop>0)
    {
        /*Decide whether swords in the holding container has a durability of one or more
          before adding them into the menu*/
        vector <baseClass*> swordWithDurabilityContainer;
        for (int i = 0; i < holdingContainer.size(); i++)
        {
            if (holdingContainer[i]->usage() > 0) {
                swordWithDurabilityContainer.push_back(holdingContainer[i]);
            }
        }

        /*Check if there's any items in the swordWithDurabilityContainer otherwise break out of the loop*/
        if (swordWithDurabilityContainer.size() == 0) { break; }

        /*Print the items*/
        cout << "Attack Options:" << endl;
        for (int i = 0; i < swordWithDurabilityContainer.size(); i++) {
            cout << i << ". " << swordWithDurabilityContainer[i]->weaponName().c_str() << endl;
        }
        /*Ask for user input*/
        cin >> choice;

        /*Check if the input is valid*/
        if (choice<0 || choice > swordWithDurabilityContainer.size() - 1) {
            cout << "Wrong option" << endl;
        }
        else {
            swordWithDurabilityContainer[choice]->attack();
        }
    }
    /*Notify the user that the loop has ended*/
    cout << "No more items in the list(swordWithDurabilityContainer variable)";
}
#包括
#包括
#包括
使用名称空间std;
类基类{
公众:
虚空攻击(){};
虚拟int用法(){return 1;}
虚拟字符串weaponName(){返回“剑x”;}
};
类剑:公共基类
{
私人:
耐用性;
字符串名;
公众:
/*设定剑的名称和耐久性*/
剑(字符串nameToSet,int dur):名称(nameToSet),耐久性(dur){}
int usage(){返回耐久性;}
/*耐久性降低1,并告知攻击中使用的剑的名称*/
无效攻击(){
耐久性--;


问题是什么?请记下并阅读。这是一个很好的列表。当您使用调试器时,是哪个语句导致了问题?变量的实际值是什么?变量的预期值是什么?请用答案编辑您的帖子。@RawN我将问题加粗,以便您和其他人可以看到it@ThomasMatthews没有显示错误,但它根据代码而不是菜单选择了剑。@TobySpeight我更新了代码问题是什么?请获取并阅读。下面是一个很好的列表。当您使用调试器时,哪个语句导致了问题?变量的实际值是什么?变量的预期值是什么变量?请用答案编辑您的帖子。@RawN我将问题加粗,以便您和其他人可以看到it@ThomasMatthews没有显示错误,但它根据代码选择剑,而不是在菜单上。@TobySpeight我更新了代码。我不知道这在我的情况下如何适用。我可以看到你试图使用ar传递对象ray,但是我如何才能使我的选择动态?这几乎解决了我的问题,但不管在第四个循环中是否使用剑C,剑C都会消失。我尝试了你的代码,它使对象成为“C”留下来,但我更新了上面的代码以检查耐久性。我在互联网上查找了当对象的变量为零时如何销毁对象。我知道这与指针有关,但我不知道如何做,因为我对该语言还是新手。此外,您在代码上面的摘要不正确。@CraftedGaming对象已创建在主作用域中,它们将在主函数退出后被销毁。如果要控制对象的创建和销毁时间,请使用指针和新运算符创建该对象的新实例。然后使用delete来销毁它。我更新了代码。它成功地将其删除,但在输入另一个选项后,当其中一个对象g