访问 Class 对象以与全局变量进行比较

Posted

技术标签:

【中文标题】访问 Class 对象以与全局变量进行比较【英文标题】:Accessing Class objects to compare with global variables 【发布时间】:2019-12-04 05:13:04 【问题描述】:

我正在制作一个文本存储菜单。我有一个基本的武器类,其中包含一个用于名称和武器声音的字符串,一个 bool 表示您是否已经拥有该物品,一个 double 表示价格。

我正在尝试将类中的双倍价格与全局变量 p_balance(玩家余额)进行比较 检查玩家是否买得起该物品

这是课程

class Weapon

public:
    string name, sound;
    bool owned = false;
    double price;

public:

    Weapon() //Default Constructor
    
        name = "name";
        sound = "sound";
        owned = owned;
        price = 0;

    

    Weapon(string i_name, string i_sound, bool i_owned, double i_price)//Constructor
    
        name = i_name;
        sound = i_sound;
        owned = i_owned;
        price = i_price;

        void displayItems();
        
            cout << name << " - " << price << "g." << endl;
        


    

;

(我知道我知道我应该将 Weapons[i] 切换到 Weapons[i] 以使我的生活更轻松)

这是我最近一次访问它的尝试

while (p_balance < Weapons[0].getPrice()) // Not enough gold
                
                    cout << "Sorry, it seems you don't have enough gold." << endl;
                    goto MainMenu;
                

我曾尝试在类中创建一个方法来获取价格,但没有成功,但这就是为什么它调用了一个不存在的方法

我也玩过 if 循环,但它变得凌乱并且显示的信息比我喜欢的要多 在所有者资金检查之后,我让它执行所有权检查并检查该项目插槽是否配备了某些东西,但是当我使用嵌套的 if 语句时,即使第一个 if (price) 失败,它也会显示所有这些语句的结果,所以我想出一个 while 语句?

我不是在寻找答案,因为我想学习,但上次我在这里发帖时,我得到了一些真正帮助我的很好的提示,我对此感到沮丧,所以我想我会问求助!

编辑:

我考虑了你们所说的,然后又回到了伪。 我对现在的情况很满意:

#include <iostream>
#include <string>
using namespace std;


int p_choice;
double p_balance = 125;

//Simple Weapon Class
class Weapon

public:
    int slot;
    string name, sound;
    bool owned = false;
    double price;



public:

    Weapon() //Default Constructor
    
        slot = 0;
        name = "name";
        sound = "sound";
        owned = owned;
        price = 0;


    

    Weapon(int i_slot, string i_name, string i_sound, bool i_owned, double i_price)//Constructor
    
        slot = i_slot;
        name = i_name;
        sound = i_sound;
        owned = i_owned;
        price = i_price;

        double* p_ptr;
        p_ptr = &price;

        void displayItems();
        
            int x = 0;

            cout << slot << " - " <<  name << " - " << price << "g." << endl;
        

    
;


int main()


    int p_selection;
    bool hasWeapon, offHand, inStore;
    hasWeapon = false;
    offHand = false;
    inStore = true;

//Weapon Array
    cout << "---------------------------------------------" << endl;
    Weapon Weapons[6];
    Weapons[0] = Weapon(1, "Great Sword of Greatening", "Tssng", false, 50);
    Weapons[1] = Weapon(2, "Claw", "Thwack", false, 10);
    Weapons[2] = Weapon(3, "Silent Pea-shooter", "Pew Pew", false, 15);
    Weapons[3] = Weapon(4, "Loudening Silencer Attachment", "...", false, 5);
    Weapons[4] = Weapon(5, "Tiger-Repelling Rock ", "Meooowraaarah", false, 25);
    Weapons[5] = Weapon(6, "The Stick of Truth", "How do you kill that which has no life", false, 100);
    cout << "---------------------------------------------" << endl;
    cout << "Welcome to my store, traveller!" << endl;
    cout << "How can I help you?" << endl;

//Menu

    do
    


        cout << "1. Buy" << endl;
        cout << "2. Check Balance" << endl;
        cout << "3. Exit" << endl;

        cin >> p_choice;
        if (p_choice == 1)
        

//Ask player which weapon the want to buy

            cout << "Which item are you interested in?" << endl;
            cout << "(Choose using 1-6)" << endl;
            cin >> p_selection;
            if (p_selection >= 1 && p_selection <= 6)
            
                int i_choice = p_selection - 1;// Take one off the choice so it corresponds correctly to the object in the array
                if (Weapons[i_choice].owned == false && p_balance >= Weapons[p_selection].price)// check if item is owned/can be bought
                
                    if (hasWeapon == false)// flip "Slot" flag
                    
                        hasWeapon = true;
                    

                    Weapons[i_choice].owned = true;// flip ownership flag
                    p_balance = p_balance - Weapons[i_choice].price; // update balance

                    cout << "Good eye, that's a fine item! Enjoy your " << Weapons[i_choice].name << endl;

                
                else if (Weapons[i_choice].price > p_balance)
                
                    cout << "Seems you're a little short on gold..." << endl;

                
                else if (Weapons[i_choice].owned == true)
                
                    cout << "Seems you don't know how to dual wield, why don't you pick something else." << endl;

                



            
        

// Check Balance
        if (p_choice == 2)
           
            cout << "Your balance is: " << p_balance << "g." << endl;
        

// Leave
        if (p_choice == 3 && inStore == true)
        
            cout << "Thanks for stopping by! Good luck out there!" << endl;
            inStore = false;

        
        else if (p_choice == 3 && hasWeapon == false)
        
            cout << "You're gonna want a weapon out there, traveller" << endl;
        
     while (inStore == true);



//Scenario
    bool scenario = true;

    cout << "Inventory" << endl;
    for (int x = 0; x < 6; x++)
    
        if (Weapons[x].owned == true)
        
            //convey slot
            cout << "Press " << Weapons[x].slot << " to use your " << Weapons[x].name << endl;
            cin >> p_choice;
        

    
    do
    
        for (int y = 0; y < 1; y++)
        
            int use = p_choice - 1;

            if (Weapons[use].owned == true)
            

                cout << Weapons[use].sound << endl;

            

        
     while (scenario == true);




    system("pause");
    return 0;



唯一的问题是它不停地播放“声音”,for循环试图让它运行1次哈哈

【问题讨论】:

在您最喜欢的 C++ 参考中搜索“初始化列表”。 我建议您更改设计以避免goto。没有适当纪律的 goto 语句可能会调用意大利面条式代码和不需要的行为。 看起来你需要花更多的时间设计你的程序而不是编码。例如,您需要确定在哪里需要循环而不是使用条件语句。 1.您在构造函数的定义中声明/定义一个函数。 2. 你在“displayItems()”的声明和定义之间多加了一个分号。 全局变量“p_balance”在哪里声明?如果在其他文件中声明,则需要搜索“extern”的用法。 【参考方案1】:

这是最终产品

#include <iostream>
#include <string>
using namespace std;


int p_choice;
double p_balance = 125;

//Simple Weapon Class
class Weapon

public:
    int slot, durability;
    string name, sound;
    bool owned = false;
    double price;



public:

    Weapon() //Default Constructor
    
        slot = 0;
        name = "name";
        sound = "sound";
        owned = owned;
        price = 0;
        durability = 0;


    

    Weapon(int i_slot, string i_name, string i_sound, bool i_owned, double i_price, int i_durability)//Constructor
    
        slot = i_slot;
        name = i_name;
        sound = i_sound;
        owned = i_owned;
        price = i_price;
        durability = i_durability;

        void displayItems();
        
            int x = 0;

            cout << slot << " - " << name << " - " << price << "g." << endl;
        

    
;


int main()


    int p_selection;
    bool hasWeapon, offHand, inStore;
    hasWeapon = false;
    offHand = false;
    inStore = true;

    //Weapon Array
    cout << "---------------------------------------------" << endl;
    cout << "For Sale:" << endl;
    Weapon Weapons[6];
    Weapons[0] = Weapon(1, "Great Sword of Greatening", "Tssng", false, 50, 25);
    Weapons[1] = Weapon(2, "Claw", "Thwack", false, 10, 15);
    Weapons[2] = Weapon(3, "Silent Pea-shooter", "Pew Pew", false, 15, 20);
    Weapons[3] = Weapon(4, "Loudening Silencer Attachment", "...", false, 5, 5);
    Weapons[4] = Weapon(5, "Tiger-Repelling Rock ", "Meooowraaarah", false, 25, 30);
    Weapons[5] = Weapon(6, "The Stick of Truth", "How do you kill that which has no life", false, 100, 50);
    cout << "---------------------------------------------" << endl;
    cout << "Welcome to my store, traveller!" << endl;
    cout << "How can I help you?" << endl;

    //Menu

    do
    


        cout << "1. Buy" << endl;
        cout << "2. Check Balance" << endl;
        cout << "3. Exit" << endl;


        cin >> p_choice;
        if (p_choice == 1)
        

            //Ask player which weapon the want to buy

            cout << "Which item are you interested in?" << endl;
            cout << "(Choose using 1-6)" << endl;
            cin >> p_selection;
            if (p_selection >= 1 && p_selection <= 6)
            
                int i_choice = p_selection - 1;// Take one off the choice so it corresponds correctly to the object in the array
                if (Weapons[i_choice].owned == false && p_balance >= Weapons[p_selection].price)// check if item is owned/can be bought
                
                    if (hasWeapon == false)// flip weapon flag
                    
                        hasWeapon = true;
                    

                    Weapons[i_choice].owned = true;// flip ownership flag
                    p_balance = p_balance - Weapons[i_choice].price; // update balance

                    cout << "Good eye, that's a fine item! Enjoy your " << Weapons[i_choice].name << endl;

                
                else if (Weapons[i_choice].price > p_balance)
                
                    cout << "Seems you're a little short on gold..." << endl;

                
                else if (Weapons[i_choice].owned == true)
                
                    cout << "Seems you don't know how to dual wield, why don't you pick something else." << endl;
                    cout << "(You already own this item)" << endl;

                




            
        

        // Check Balance
        if (p_choice == 2)
        
            cout << "Your balance is: " << p_balance << "g." << endl;
        

        // Leave
        if (p_choice == 3 && inStore == true)
        
            cout << "Thanks for stopping by! Good luck out there!" << endl;
            inStore = false;

        
        else if (p_choice == 3 && hasWeapon == false)
        
            cout << "You're gonna want a weapon out there, traveller" << endl;
        
     while (inStore == true);



    //Scenario
    bool scenario, equipped;
    int attack, equip, choose;

    equipped = false;
    scenario = true;

    do
    

    Equip:
        cout << "----------------------------------------------------" << endl;
        cout << "                Inventory" << endl;
        for (int x = 0; x < 6; x++)
        
            if (Weapons[x].owned == true)
            
                //convey slot and display equippable items
                cout << Weapons[x].slot << " - " << Weapons[x].name << " - Uses Left: " << Weapons[x].durability << endl;

            

        
        cout << "----------------------------------------------------" << endl;
        cout << endl;
        cout << "What would you like to equip? (Select using the given number + Enter)" << endl;
        cout << "(Press 9 to quit)" << endl;
        cin >> choose;
        equip = choose - 1;
        if (choose == 9)
        
            scenario = false;
        
        else if (Weapons[equip].owned == true)
        
            int action;
            cout << "You have equipped " << Weapons[equip].name << endl;
        Action:
            cout << "Press 1 and Enter to use." << endl;
            cout << "Press 2 and Enter to equip a new item" << endl;
            cout << "Press 3 and Enter to go home" << endl;
            cin >> action;


            if (Weapons[equip].durability <= 0)// cant use broken items
            
                cout << "That item appears to have broken!" << endl;

            
            else if (action == 1 && Weapons[equip].durability >= 0)// attack and durability check
            
                cout << Weapons[equip].sound << endl;
                cout << endl;
                Weapons[equip].durability--;// durability decrement
                goto Action;

            
            else if (action == 2)// change equip
            
                goto Equip;
            
            else if (action == 3)// exit
            
                scenario = false;
            

        


     while (scenario == true);

    cout << endl;
    cout << "I wish you a safe journey home, traveller." << endl;



    system("pause");
    return 0;


再次感谢您的指导。

【讨论】:

以上是关于访问 Class 对象以与全局变量进行比较的主要内容,如果未能解决你的问题,请参考以下文章

无法获取全局变量

多线程访问全局变量和局部变量

解决全局变量污染问题

全局变量怎么定义

访问/拦截 Nashorn 的全局对象变量

nodejs学习笔记 -- 全局对象与全局变量