How to stop money from going over

Pages: 1... 34567
Hmm well been a while I dont even use []

How about
before calling Player pc( );
you put something like bool test[2] = { false , false };
then put that when you create the object instead of { false , false };

I use pointers/vectors
i'll just use a pointer, i would like to start using them as i never used them in my programs before aside from just testing and playing around with them. So how would i go about doing that?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

class Test
{
public:
    Test( bool* );
private:
    bool *test;
};

Test::Test( bool *test ) : test( test ){};

int main()
{
    bool t2[2] = { true , true };
    Test t( t2 );
}


here is one way. There might be better ways though.
Ok i changed everything and it compiles without warnings or errors did i do everything right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include "PlayerClass.h"

using namespace std;

Player::Player(int MONEY, std::string NAME, int EXPERIENCE,
               int ACCOUNTSHACKED, int PASSWORDCRACKERLVL,
               int DECRYPTIONTOOLLVL, int PASSWORDCRACKERPRICE,
               int DECRYPTIONTOOLPRICE, bool *PLR_INVENTORY): money(MONEY), name(NAME),
                                                               experience(EXPERIENCE),
                                                               accountsHacked(ACCOUNTSHACKED),
                                                               PasswordCrackerLvl(PASSWORDCRACKERLVL),
                                                               DecryptionToolLvl(DECRYPTIONTOOLLVL),
                                                               PasswordCrackerPrice(PASSWORDCRACKERPRICE),
                                                               DecryptionToolPrice(DECRYPTIONTOOLPRICE),
                                                               plr_inventory(PLR_INVENTORY){}

Player::~Player(){}


int main()
{
    bool plr_inv[2] = {false, false};
    Player pc(1200, "Default", 0, 0, 1, 1, 100, 150, plr_inv);

    pc.startup();
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef PLAYERCLASS_H_INCLUDED
#define PLAYERCLASS_H_INCLUDED

#include <string>
#include <vector>

class Player
{
    public:
    Player(int MONEY, std::string NAME, int EXPERIENCE,
           int ACCOUNTSHACKED, int PASSWORDCRACKERLVL,
           int DECRYPTIONTOOLLVL, int PASSWORDCRACKERPRICE,
           int DECRYPTIONTOOLPRICE, bool *PLR_INVENTORY
          );
    ~Player();
    void gameStart();
    void save();
    void load();
    void startup();
    int mainGame();
    void accountNumbers();
    void shop();
    void cheats();
    void playerInfo();
    void hackBank();
    void upgrades();
    void alabamaBank();
    void hackingMethod();

    private:
        int money;
        std::string name; //The player will enter their name so name will not be constant
        bool isFirstStartup;
        std::vector<std::string> createNumberList;
        std::vector<std::string> bankList;
        std::vector<float> bankAccounts; //this stores the money in the list.
        int experience;
        int accountsHacked;
        int PasswordCrackerLvl;
        int DecryptionToolLvl;
        int PasswordCrackerPrice;
        int DecryptionToolPrice;
        bool *plr_inventory;
};

#endif // PLAYERCLASS_H_INCLUDED 
There's always std::array for fixed size arrays like your plr_inventory.
i would rather use a pointer
Well he might get more items later on but I do agree he could use any of the stl containers if he wanted http://www.cplusplus.com/reference/stl/ but what ever works for him is okay with me. I don't mind using pointers for simple stuff.
Last edited on
I usually only use arrays when I am putting user-noninterfaceable items in there, like an array of function pointers, or an array containing different output messages that are dependent upon some state or another, and so I can avoid the whole switch/if-else situation altogether.
precisely, so does it look good? also i have a question about pointers with classes, im used to seeing pointers like this:

1
2
3
4
int num = 500;
int *p_int = &num;

cout << *p_int << endl;


This i understand completley, a number is created, then a pointer is created and the numbers memory address is assigned to the pointer and then dereferenced in the cout statement but i really cant figure out whats going on in the class with the pointers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include "PlayerClass.h"

using namespace std;

Player::Player(int MONEY, std::string NAME, int EXPERIENCE,
               int ACCOUNTSHACKED, int PASSWORDCRACKERLVL,
               int DECRYPTIONTOOLLVL, int PASSWORDCRACKERPRICE,
               int DECRYPTIONTOOLPRICE, bool *PLR_INVENTORY):  money(MONEY), name(NAME),
                                                               experience(EXPERIENCE),
                                                               accountsHacked(ACCOUNTSHACKED),
                                                               PasswordCrackerLvl(PASSWORDCRACKERLVL),
                                                               DecryptionToolLvl(DECRYPTIONTOOLLVL),
                                                               PasswordCrackerPrice(PASSWORDCRACKERPRICE),
                                                               DecryptionToolPrice(DECRYPTIONTOOLPRICE),
                                                               plr_inventory(PLR_INVENTORY){}

Player::~Player(){}


int main()
{
    bool plr_inv[2] = {false, false};
    Player pc(1200, "Default", 0, 0, 1, 1, 100, 150, plr_inv);

    pc.startup();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef PLAYERCLASS_H_INCLUDED
#define PLAYERCLASS_H_INCLUDED

#include <string>
#include <vector>

class Player
{
    public:
    Player(int MONEY, std::string NAME, int EXPERIENCE,
           int ACCOUNTSHACKED, int PASSWORDCRACKERLVL,
           int DECRYPTIONTOOLLVL, int PASSWORDCRACKERPRICE,
           int DECRYPTIONTOOLPRICE, bool *PLR_INVENTORY
          );
    ~Player();
    void gameStart();
    void save();
    void load();
    void startup();
    int mainGame();
    void accountNumbers();
    void shop();
    void cheats();
    void playerInfo();
    void hackBank();
    void upgrades();
    void alabamaBank();
    void hackingMethod();

    private:
        int money;
        std::string name; //The player will enter their name so name will not be constant
        bool isFirstStartup;
        std::vector<std::string> createNumberList;
        std::vector<std::string> bankList;
        std::vector<float> bankAccounts; //this stores the money in the list.
        int experience;
        int accountsHacked;
        int PasswordCrackerLvl;
        int DecryptionToolLvl;
        int PasswordCrackerPrice;
        int DecryptionToolPrice;
        bool *plr_inventory;
};

#endif // PLAYERCLASS_H_INCLUDED
I am totally not sure what you're asking. Especially with
but i really cant figure out whats going on in the class with the pointers.
ok sorry so ok here i have

bool *PLR_INVENTORY

in the constructor. I also have it again in the constructor in the class

1
2
3
4
5
Player(int MONEY, std::string NAME, int EXPERIENCE,
           int ACCOUNTSHACKED, int PASSWORDCRACKERLVL,
           int DECRYPTIONTOOLLVL, int PASSWORDCRACKERPRICE,
           int DECRYPTIONTOOLPRICE, bool *PLR_INVENTORY
          );



why is that?

and lastly why is it created again here in private

bool *plr_inventory;

from my POV it seems as though its creating the nitial pointer in all of these 3 spots but i know i must be wrong, please explain.
Giblit wrote:
Well he might get more items later on...


CH1156 wrote:
...the bool array isa fixed size ...

That is why I recommended std::array. I am guessing it merely flips on or off to tell if an item is present in the inventory.

Edit:
Emphasis.
Last edited on
Now I'm completely confused. You're not sure why you put those pointers in there? Did someone else write that code for you or something?
pretty much.
Oh I didn't realize he said a fixed size I figured his player would pick up stuff off the ground later on and drop things as he went.
no thats for when you buy the items at the shop, they are set to true.
So anyways what about those pointers.
What about them? I'm confused lol.

ok sorry so ok here i have

bool *PLR_INVENTORY

in the constructor. I also have it again in the constructor in the class


Player(int MONEY, std::string NAME, int EXPERIENCE,
int ACCOUNTSHACKED, int PASSWORDCRACKERLVL,
int DECRYPTIONTOOLLVL, int PASSWORDCRACKERPRICE,
int DECRYPTIONTOOLPRICE, bool *PLR_INVENTORY
);



why is that?

and lastly why is it created again here in private

bool *plr_inventory;

from my POV it seems as though its creating the nitial pointer in all of these 3 spots but i know i must be wrong, please explain.
Last edited on
Pages: 1... 34567