-1

I have the following files:

main.cpp
shop.hpp
player.hpp

With the following code in each of them:

main.ccp:

 #include <iostream>
    #include <cstdlib>
    #include "shop.hpp"

    using namespace std;
    string *inventory= new string[3];
    int invGold= 355;

    int main(void){
    shop store;
    store.store();
    }

shop.hpp:

#include <iostream>
#include <cstdlib>

using namespace std;

class shop{
    public:
    string shopOption;
        string shopOptions[6]= {"Buy", "buy", "Sell", "sell", "Leave", "leave"};
        string shopInv[3]= {"Sword", "Potion", "Arrows x 25"};
        int shopInvAmount= sizeof(shopInv)/sizeof(shopInv[0]);
        int shopPrices[3]= {250, 55, 70};

    shop(){
        cout << "Shopkeeper: We buy, we sell, what's your buisness?" << endl;
    }
    void store(void){
        getline(cin,shopOption);

        if(shopOption.compare(shopOptions[0]) == 0 || shopOption.compare(shopOptions[1]) == 0){
            buy();
        }

        else if(shopOption.compare(shopOptions[2]) == 0 || shopOption.compare(shopOptions[3]) == 0){
            sell();
        }

        else if(shopOption.compare(shopOptions[4]) == 0 || shopOption.compare(shopOptions[5]) == 0){
            leave();
        }
    }

    void buy(){
        srand(time(0));
        string buyQuotes[3]= {"What are you buyin', hon?", "Make it quick, I ain't got all day.", "Another day, another sell."};
        int quotePick= rand() % sizeof(buyQuotes)/sizeof(buyQuotes[0]) - 1;
        if (quotePick < 0){
            quotePick= 0;
        }

        else if (quotePick > (sizeof(buyQuotes)/sizeof(buyQuotes))){
            quotePick= sizeof(buyQuotes)/sizeof(buyQuotes);
        }
        cout << "TEST:" << sizeof(shopInv)/sizeof(shopInv[0]) << endl;
        cout << buyQuotes[quotePick] << endl;
        cout << "SHOP INVENTORY" << endl << "--------------" << endl;
        cout << endl;
        for (int i=0; i < sizeof(shopInv)/sizeof(shopInv[0]); i++){
            cout << shopInv[i]<< ": " << shopPrices[i] << endl;
        }
        cout << endl << "What'll it be?:";
        getline(cin,shopOption);
    }
    void sell(){

    }

    void leave(){

    }
};

and player.hpp

class player{
    public:
    int playerHP= 18;
    string playerInv[5] {};
    int playerGold= 355;
};

Now, what i'd like to do, is that after the character selects the items they want to buy, and te amount of it, (Not programmed yet) check the price of the combined items, and see if the character has enough money on hand, and if the character buys the items, add them to the player's inventory.

But i'd like to keep the values the store uses, and everything related to the player in different class files. Thing is, I have no idea how to pull something like that.

So, is t possible to access a class' variable from another class that is in another file althogether? And if isn't, how would you suggest i get around this problem?

Arizodo
  • 3
  • 1

1 Answers1

0

Start reading here: How does the compilation/linking process work? to get multiple files working for you. Odds are pretty good that whatever coding environment you are using will automate the process for you.

Then consider making an item class

class Item
{
public:
    Item(string name, int price): mName(name), mPrice(price)
    {
    }
    string getName()
    {
        return mName;
    }
    string getPrice()
    {
        return mPrice;
    }
    // other functions
private:
    string mName;
    int mPrice;
    // other stuff
}

In Shop and Player, keep a list of Items

vector<Item> items;

When a Player tries to buy an item, find it in the list, ensure the Player can afford it, remove it from the Shop's list and add it to the Player's list.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • To be honest, I'm still very much learning C++, I haven't learned about yet, so i'm gonna have to read up on that. A question: What does putting something after a ':' after a constructor do, exactly? This is the first time i see anything like this. As for the Development Enviroment, I really only use Gedit/Vim/Notepad++ and MinGW/Make, so i doubt anything is automated, why is that an important factor, exactly? – Arizodo Jul 11 '15 at 20:36
  • @Arizodo The `:` tells the compiler that there's a list of member variables to be initialized, so `mName(name)` assigns the parameter name to the class member mName before the constructor starts running the code inside the braces. Really important when you have a subclass that inherits from a base class and the base class needs to be initialized before the subclass. Development environment just makes your job easier by performing some tasks, like arranging for a multiple file project to build correctly. Make is ultimately more powerful/versatile, but has a steeper learning curve. – user4581301 Jul 11 '15 at 22:16
  • Thanks, i'm gonna have to mess around with that soon. – Arizodo Jul 11 '15 at 22:31