How to stop money from going over

Pages: 1... 567
so what exactly do i need to look at? i know about function i dont need that, so this?
http://www.cplusplus.com/doc/tutorial/pointers/
I'm suggesting that you read the entirety of the source files for the code that you're trying to troubleshoot, and if you have any trouble understanding what's going on, look it up in the /reference/ or /doc/ directories on this site. If you find something that you still can't understand even after looking it up, come back down to the forums and ask more questions. We'll still help you out.
Forget it, im just going to use a vector. this threat started out with how to do a class the right way and ended up with pointers so thats not good, i want to stay on topic. so ive changed everything but im not sure if this is right, i think so but i want to be sure

main

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
#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, std::vector<bool> PLR_CHECKINVENTORY
              ):

               money(MONEY), name(NAME),
               experience(EXPERIENCE),
               accountsHacked(ACCOUNTSHACKED),
               PasswordCrackerLvl(PASSWORDCRACKERLVL),
               DecryptionToolLvl(DECRYPTIONTOOLLVL),
               PasswordCrackerPrice(PASSWORDCRACKERPRICE),
               DecryptionToolPrice(DECRYPTIONTOOLPRICE),
               plr_CheckInventory(PLR_CHECKINVENTORY)
{
//Empty
}

Player::~Player(){}


int main()
{
    vector<bool> plr_inv;

    plr_inv[0] = false;
    plr_inv[1] = false;

    Player pc(1200, "Default", 0, 0, 1, 1, 100, 150, plr_inv);

    pc.startup();
}


PlayerClass


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, std::vector<bool> PLR_CHECKINVENTORY
              );
        ~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,
            experience,
            accountsHacked,
            PasswordCrackerLvl,
            DecryptionToolLvl,
            PasswordCrackerPrice,
            DecryptionToolPrice;

        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.
        std::vector<bool> plr_CheckInventory;
};

#endif // PLAYERCLASS_H_INCLUDED 


game

1
2
3
4
5
6
7
8
9
#include <iostream>
#include "PlayerClass.h"

using namespace std;

void Player::startup()
{
    cout << "Startup sequence" << endl;
}
Last edited on
Also i cant output anything, my program just crashes

game.cpp

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "PlayerClass.h"

using namespace std;

void Player::startup()
{
    cout << "Startup sequence" << endl;

    cout << plr_CheckInventory[0];
    cout << name << endl;
}
Last edited on
You created a vector with 0 size on line 29 in main(). Attempting to access space that is not there will be a problem.

I think you meant:
1
2
3
4
5
6
7
8
vector<bool> plr_inv;

plr_inv.push_back(false);
plr_inv.push_back(false);

//Or...

vector<bool> plr_inv(2, false);
ah ok cool ive been messign with arrays all day i guess it just got mixed up. thanks.
ok now i have another problem, when i enter the load instructions for loading the game it gives me an error but it doesnt when i enter the save

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



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
#ifndef PLAYER_CLASS_H_INCLUDED
#define PLAYER_CLASS_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, std::vector<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;
        std::vector<bool> plr_inventory;
};
#endif // PLAYER_CLASS_H_INCLUDED 




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
void Player::load()
{
    ifstream loadGame;
    loadGame.open("game.txt");

    if(loadGame.fail())
    {
        isFirstStartup = true;
    }

    if(!loadGame.fail())
    {
        getline(loadGame, name);
        loadGame >> money;
        loadGame >> isFirstStartup;
        loadGame >> experience;
        loadGame >> accountsHacked;
        loadGame >> PasswordCrackerLvl;
        loadGame >> DecryptionToolLvl;
        loadGame >> PasswordCrackerPrice;
        loadGame >> DecryptionToolPrice;
        loadGame >> plr_inventory[0];
        loadGame >> plr_inventory[1];

        loadGame.close();
    }
}
Last edited on
I'm going to repeat myself, and I don't often do this, but you seem to not be understanding what I'm getting at.

Your problem is that your code is smarter than you.

One of the basic principles of programming is never to do something completely and totally ingenious unless it's either simple enough for you to understand after 3 beers or you can easily replicate it at the drop of a hat, and at will.

That said, again, your code is outsmarting you. You need to go back and thoroughly review what you've done, and only once you know what's already there will you be able to progress.
I honestly think he should just read about prototyping , function passing by reference/copy/pointer , class members.

Your problem might be line 13 in your load also it's hard to tell without your save though...
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
void Player::save()
{
    ofstream saveGame;
    saveGame.open("game.txt");

    isFirstStartup = false;

    if(!saveGame.fail())
    {
        saveGame << name << endl;
        saveGame << money << endl;
        saveGame << isFirstStartup << endl;
        saveGame << experience << endl;
        saveGame << accountsHacked << endl;
        saveGame << PasswordCrackerLvl << endl;
        saveGame << DecryptionToolLvl << endl;
        saveGame << PasswordCrackerPrice << endl;
        saveGame << DecryptionToolPrice << endl;
        saveGame << plr_inventory[0] << endl;
        saveGame << plr_inventory[1] << endl;

        cout << "\nGame saved successfully\n" << endl;
        saveGame.close();
    }
    if(saveGame.fail())
    {
        std::cout << "\nError game failed to save to file\n" << endl;
    }
}
hmm well if name contains no spaces you could just use operator >> instead but I don't think that is your problem. What exactly is your error message?
Also shouldn't you set isFirstStartup to false if it loads in the data?


Actually your error is on line 22 and 23 of your load..I am not sure why I didn't notice this before..

What you want to do is read them into a temp variable like this
1
2
3
4
5
6
bool temp = false;

loadGame >> temp;
plr_inventory.push_back( temp );
loadGame >> temp;
plr_inventory.push_back( temp );


You can't insert items into a vector like that since the vector is of size 0.

Another solution would be to change the size of the vector before loading in the data
1
2
3
4
5
6
7
8
9
10
11
12
//1)
plr_inventory.push_back();
plr_inventory.push_back();

//2)
plr_inventory.resize(2);

//3)
plr_inventory.reserve( 2 );

loadGame >> plr_inventory[0];
loadGame >>plr_inventory[1];
C:\Users\Chay Hawk\Desktop\Anti Piracy Program\Save_Load.cpp|59|error: no match for 'operator>>' in 'loadGame >> ((Player*)this)->Player::plr_inventory.std::vector<bool, _Alloc>::operator[]<std::allocator<bool> >(0u)'|

C:\Users\Chay Hawk\Desktop\Anti Piracy Program\Save_Load.cpp|60|error: no match for 'operator>>' in 'loadGame >> ((Player*)this)->Player::plr_inventory.std::vector<bool, _Alloc>::operator[]<std::allocator<bool> >(1u)'|
okay then do what I mentioned earlier with the temp variable.
I believe the problem comes from the fact that vector<bool> is a specialized template. When you use at() or operator[], the function returns a proxy--a representation of an element--for which operator>> has not been defined.

The reason why vector<bool> is specialized is because the proxies are supposed to represent bits of a byte.

Edit:
Besides the vector<bool> specialization, there is also the bitset container for holding a fixed-sized array of bits/flags. It is like having a container/array of bools.

I'll go away now.
Last edited on
Ok well i got it all working, thanks.
Topic archived. No new replies allowed.
Pages: 1... 567