How to stop money from going over

Pages: 1234567
Should be fine and you can overload your class object instead of line 42-43 on the start.

1
2
3
4
5
6
7
8
//test class.h under the public: members
friend std::ostream& operator<<( std::ostream& , const Player& );

//test class.cpp
std::ostream& operator<<( std::ostream &stm , const Player &plr )
{
    return( stm << "Name - " << plr.plr_name << "\nHealth - " << plr.plr_health );
}


As for your first question you would use a default constructor when ever you would do this:
int x; //undefined
std::string str; //undefined

There are other purposed of it though. Your close should have a default constructor even when you don't declare one though. The nice thing about declaring one is that you can tell it what to do if you want to have default values like instead of it starting as undefined you could have like 100 health and say the name "Fred"

*forgot the plr. in the operator
Last edited on
I dont know what any of this does:

1
2
3
4
5
6
7
8
//test class.h under the public: members
friend std::ostream& operator<<( std::ostream& , const Player& );

//test class.cpp
std::ostream& operator<<( std::ostream &stm , const Player &plr )
{
    return( stm << "Name - " << plr.plr_name << "\nHealth - " << plr.plr_health );
}


also im a little confused about what you said, i dont understand any of it. could you be more clear and precise please?
Last edited on
Pretty much saying even if you do not create your own default constructor there shoudl still be one.

And I just overloaded the operator<< for the output stream ( cout )
so you can do something like
std::cout << plr1 << std::endl;

and it has to be a friend so you can access the private members.
What helped me a lot was reading about classes and constructors from the tutorial on this site. Then play with the new concept. You can also read about friend classes.

Right now I am in the template section of the tutorial. Slowly I will get through the whole thing.
"Pretty much saying even if you do not create your own default constructor there shoudl still be one."

Like this?

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
48
49
50
#include "Test_Class.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

Player::Player()
{
    
}

Player::Player(int hp, std::string name) : plr_health(hp),
                                           plr_name(name){}

Player::~Player()
{

}

void Player::load()
{
    ifstream fileIn;
    fileIn.open("file.txt");

    getline(fileIn, plr_name);
    fileIn >> plr_health;
}

void Player::save()
{
    ofstream fileOut;
    fileOut.open("file.txt");

    fileOut << plr_name << endl;
    fileOut << plr_health << endl;
}

void Player::start()
{
    load();
    cout << "This is the beginning" << endl;
    cout << "Current player name " << plr_name << endl;
    getline(cin, plr_name);

    cout << "New Name - " << plr_name << endl;
    cout << "Health " << plr_health << endl;

    save();
}


"What helped me a lot was reading about classes and constructors from the tutorial on this site."

I looked through tutorials on this site, they dont help much their too technical for me to understand.
I mean if you do not have a constructor like Player()

It will by default create it.

so you can still call
Player p1;

http://www.learncpp.com/
check out chapter 8 it is more in depth than here I think.
He has a constructor Player(int, string). A default constructor will not be generated by the compiler.
ok well i think im just going to stick with one constructor for now and then ill move on if i need to.
ok now i am trying to make the changes to my actual game and im having problems, the code in my main.cpp is too long to post here so ill just post my constructor

1
2
3
4
5
6
7
8
9
10
11
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){}


main()

1
2
3
4
5
6
7
int main()
{
    Player p(1200, "Default", 0, 0, 1, 1, 100, 150);
    p.startup();

    return 0;
}



Player_class.h

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
#ifndef PLAYER_CLASS_H_INCLUDED
#define PLAYER_CLASS_H_INCLUDED

#include <string>
#include <vector>

class Player
{
    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 // PLAYER_CLASS_H_INCLUDED 


errors

C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp||In constructor 'Player::Player(int, std::string, int, int, int, int, int, int, bool*)':|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|22|error: array used as initializer|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|380|error: no matching function for call to 'Player::Player(int, const char [8], int, int, int, int, int, int)'|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|380|note: candidates are:|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|12|note: Player::Player(int, std::string, int, int, int, int, int, int, bool*)|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|12|note: candidate expects 9 arguments, 8 provided|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\Player_Class.h|7|note: Player::Player(const Player&)|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\Player_Class.h|7|note: candidate expects 1 argument, 8 provided|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|24|error: 'Player::~Player()' is private|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|380|error: within this context|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|60|error: 'void Player::startup()' is private|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|381|error: within this context|
||=== Build finished: 6 errors, 0 warnings (0 minutes, 0 seconds) ===|
Last edited on
I'll try helpping you trim down the errors:

1) Member arrays like your plr_inventory[] variable must have a known size. You cannot declare them like that. I suggest having some constant to represent the maximum size of the inventory. Or better yet, use a vector container as you did with other variables.

2) The default field in a class is private. So right now, every single one of your class members is private and inaccessible. Simply add public: after line 8.
oh ok, i forgot public:, the class was a struct before and i forgot to put it back in there. so anyways like this?

1
2
3
4
5
6
7
8
9
10
11
12
Player::Player(int MONEY, std::string NAME, int EXPERIENCE,
               int ACCOUNTSHACKED, int PASSWORDCRACKERLVL,
               int DECRYPTIONTOOLLVL, int PASSWORDCRACKERPRICE,
               int DECRYPTIONTOOLPRICE, bool PLR_INVENTORY[2]): money(MONEY), name(NAME),
                                                               experience(EXPERIENCE),
                                                               accountsHacked(ACCOUNTSHACKED),
                                                               PasswordCrackerLvl(PASSWORDCRACKERLVL),
                                                               DecryptionToolLvl(DECRYPTIONTOOLLVL),
                                                               PasswordCrackerPrice(PASSWORDCRACKERPRICE),
                                                               DecryptionToolPrice(DECRYPTIONTOOLPRICE),
                                                               plr_inventory[2](plr_inventory[2]){}


i still get

C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp||In constructor 'Player::Player(int, std::string, int, int, int, int, int, int, bool*)':|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|22|error: expected '(' before '[' token|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|22|error: expected '{' before '[' token|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|22|error: expected unqualified-id before '[' token|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|380|error: no matching function for call to 'Player::Player(int, const char [8], int, int, int, int, int, int)'|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|380|note: candidates are:|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|12|note: Player::Player(int, std::string, int, int, int, int, int, int, bool*)|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|12|note: candidate expects 9 arguments, 8 provided|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\Player_Class.h|7|note: Player::Player(const Player&)|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\Player_Class.h|7|note: candidate expects 1 argument, 8 provided|
||=== Build finished: 4 errors, 0 warnings (0 minutes, 0 seconds) ===|
Last edited on
Look at your constructor it takes 9 parameters you are creating the object with only 8 you are forgetting the inventory. Also line 11 should be plr_inventory( PLR_INVENTORY ) and not plr_inventory( plr_inventory) Also I would suggest either a pointer for your inventory or a vector and not use [].

Ps why is your maingame returning an int? Is it returning a 1/0 as in succeeded/failed?


*edit when I say creating the object i mean in your main function line 3 it is missing the last parameter.
Last edited on
Well, think about how you initialize arrays:
 
int arr[4] = {1,2,3,4};


You certainly would not do this:
1
2
3
int arr[4] = {1,2,3,4};

int arr2[4] = arr[4];


To initialize the array in a constructor, you've got two options:
1
2
3
4
5
6
7
8
9
10
11
//Not entirely sure if this one is correct
Player::Player(/*...*/)
//...
  , plr_inventory[2]({PLR_INVENTORY[0], PLR_INVENTORY[1]})
{}

Player::Player(/*...*/)
{
   for(size_t i(0); i < 2; ++i)
      plr_inventory[i] = PLR_INVENTORY[i];
}


As for your second error in main, it is as your compiler states: you did not provide enough arguments. I see that you enter arguments for all but PLR_INVENTORY.
I am assuming you want the inventory to be empty. There are two ways to do this:
1
2
3
4
5
6
7
8
9
10
// 1) Provide a default value
Player::Player(/*...*/, bool PLR_INVENTORY[2] = {}){/*...*/}
//This allows you to keep your line in main() unchanged

// 2) Pass an empty array each time
int main(){
//...
   Player p(1200, "Default", 0, 0, 1, 1, 100, 150, {});
//...
}
ok so like this?

1
2
3
4
5
6
7
int main()
{
    Player p(1200, "Default", 0, 0, 1, 1, 100, 150, false, false);
    p.startup();

    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
Player::Player(int MONEY, std::string NAME, int EXPERIENCE,
               int ACCOUNTSHACKED, int PASSWORDCRACKERLVL,
               int DECRYPTIONTOOLLVL, int PASSWORDCRACKERPRICE,
               int DECRYPTIONTOOLPRICE, bool PLR_INVENTORY[2]): money(MONEY), name(NAME),
                                                               experience(EXPERIENCE),
                                                               accountsHacked(ACCOUNTSHACKED),
                                                               PasswordCrackerLvl(PASSWORDCRACKERLVL),
                                                               DecryptionToolLvl(DECRYPTIONTOOLLVL),
                                                               PasswordCrackerPrice(PASSWORDCRACKERPRICE),
                                                               DecryptionToolPrice(DECRYPTIONTOOLPRICE),
                                                               plr_inventory(PLR_INVENTORY[0], PLR_INVENTORY[1]){}


also what about the pointer rout?
Does it compile like that? I would assume that line 3 would have to be { false , false }

*edit
also I don't know what you mean by
pointer rout
Last edited on
no it doesnt compile i still get errors. I meant you said something about using pointers instead of an array.

C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp||In constructor 'Player::Player(int, std::string, int, int, int, int, int, int, bool*)':|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|22|error: expression list treated as compound expression in mem-initializer [-fpermissive]|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|22|warning: value computed is not used [-Wunused-value]|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|22|warning: left operand of comma operator has no effect [-Wunused-value]|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|22|error: incompatible types in assignment of 'bool' to 'bool [0]'|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|380|error: no matching function for call to 'Player::Player(int, const char [8], int, int, int, int, int, int, bool, bool)'|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|380|note: candidates are:|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|12|note: Player::Player(int, std::string, int, int, int, int, int, int, bool*)|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\main.cpp|12|note: candidate expects 9 arguments, 10 provided|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\Player_Class.h|7|note: Player::Player(const Player&)|
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\Player_Class.h|7|note: candidate expects 1 argument, 10 provided|
||=== Build finished: 3 errors, 2 warnings (0 minutes, 0 seconds) ===|
Last edited on
did you try putting curly braces around the boolean values?
in main? or in the prototyp? if in main, then yes i did, i put my code in another project file to make it easier to work with

Main.cpp

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
#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[2]): money(MONEY), name(NAME),
                                                               experience(EXPERIENCE),
                                                               accountsHacked(ACCOUNTSHACKED),
                                                               PasswordCrackerLvl(PASSWORDCRACKERLVL),
                                                               DecryptionToolLvl(DECRYPTIONTOOLLVL),
                                                               PasswordCrackerPrice(PASSWORDCRACKERPRICE),
                                                               DecryptionToolPrice(DECRYPTIONTOOLPRICE),
                                                               plr_inventory(PLR_INVENTORY[0], PLR_INVENTORY[1]){}

Player::~Player(){}


int main()
{
    Player pc(1200, "Default", 0, 0, 1, 1, 100, 150, {false, false});

    pc.start();
}


PlayerClass.h

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 



errors

C:\Users\Chay Hawk\Desktop\Modular testing\main.cpp||In constructor 'Player::Player(int, std::string, int, int, int, int, int, int, bool*)':|
C:\Users\Chay Hawk\Desktop\Modular testing\main.cpp|16|error: expression list treated as compound expression in mem-initializer [-fpermissive]|
C:\Users\Chay Hawk\Desktop\Modular testing\main.cpp|16|warning: value computed is not used [-Wunused-value]|
C:\Users\Chay Hawk\Desktop\Modular testing\main.cpp|16|warning: left operand of comma operator has no effect [-Wunused-value]|
C:\Users\Chay Hawk\Desktop\Modular testing\main.cpp|16|error: incompatible types in assignment of 'bool' to 'bool [0]'|
C:\Users\Chay Hawk\Desktop\Modular testing\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Modular testing\main.cpp|23|warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]|
C:\Users\Chay Hawk\Desktop\Modular testing\main.cpp|23|error: no matching function for call to 'Player::Player(int, const char [8], int, int, int, int, int, int, <brace-enclosed initializer list>)'|
C:\Users\Chay Hawk\Desktop\Modular testing\main.cpp|23|note: candidates are:|
C:\Users\Chay Hawk\Desktop\Modular testing\main.cpp|6|note: Player::Player(int, std::string, int, int, int, int, int, int, bool*)|
C:\Users\Chay Hawk\Desktop\Modular testing\main.cpp|6|note: no known conversion for argument 9 from '<brace-enclosed initializer list>' to 'bool*'|
C:\Users\Chay Hawk\Desktop\Modular testing\PlayerClass.h|7|note: Player::Player(const Player&)|
C:\Users\Chay Hawk\Desktop\Modular testing\PlayerClass.h|7|note: candidate expects 1 argument, 9 provided|
C:\Users\Chay Hawk\Desktop\Modular testing\main.cpp|25|error: 'class Player' has no member named 'start'|
||=== Build finished: 4 errors, 3 warnings (0 minutes, 0 seconds) ===|
oh line 16 needs curly braces also
ps I would also give line 43 a size eg [2] or w.e but if it is a dynamic size then pointer as I mentioned earlier or a vector.
Last edited on
ok but i still get errors when i fixed it?

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
#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[2]): money(MONEY), name(NAME),
                                                               experience(EXPERIENCE),
                                                               accountsHacked(ACCOUNTSHACKED),
                                                               PasswordCrackerLvl(PASSWORDCRACKERLVL),
                                                               DecryptionToolLvl(DECRYPTIONTOOLLVL),
                                                               PasswordCrackerPrice(PASSWORDCRACKERPRICE),
                                                               DecryptionToolPrice(DECRYPTIONTOOLPRICE),
                                                               plr_inventory({PLR_INVENTORY[0], PLR_INVENTORY[1]}){}

Player::~Player(){}


int main()
{
    Player pc(1200, "Default", 0, 0, 1, 1, 100, 150, {false, false});

    pc.start();
}

i gave line 43 a size also, and no the bool array isa fixed size so i guess a pointer isnt needed.

C:\Users\Chay Hawk\Desktop\Modular testing\main.cpp|23|error: no matching function for call to 'Player::Player(int, const char [8], int, int, int, int, int, int, <brace-enclosed initializer list>)'|
Last edited on
Pages: 1234567