Program Help

Pages: 12
U NEED AN EMPTY CONSTRUCTOR BECAUSE U CREATE THE OBJECT WITH A DEFAULT TYPE OF CONSTRUCTOR


vars vo;//THIS MEANS THE SAME HAS

vars vo=vars();

//* so if you want to use your previous contructor then
vars vo=vars(23.3,2, "strPN", " VarStr");


understand !!!
ok, well now everything works but when i got to output money on the screen it gives me this massive number... why is that?

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
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class vars
{
    public:
    int game();

    vars(long long int M, int XP, string PN, string BN);
    vars(){}
    long long int getMoney()
    {return money;}
    int getXP()
    {return xp;}
    string getPname()
    {return Pname;}
    string getBname()
    {return Bname;}

    private:
        long long int money;
        int xp;
        string Pname;
        string Bname;
};

int main()
{
    int choice;

    vars vo;

    cout << "1) Load" << endl;
    cout << "2) New" << endl;
    cin >> choice;

    switch(choice)
    {
        case 1:
            cout << "Welcome, in this game you make your own shop from the ground up and" << endl;
            cout << "build a mega store\n" << endl;

            cout << "Lets start by getting some information" << endl;
            break;
        case 2:
            vo.game();
            break;
    }
}

int vars::game()
{
    cin >> Pname;
    cout << money << endl;
    cout << Pname << endl;
}
Because you haven't set the value for it, so to access it is undefined behaviour, and as such could result in numerous values, such as the previous value stored at the memory location of money.
Last edited on
I tried this but still gives me a huge number

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
class vars
{
    public:
    int game();

    vars(long long int M, int XP, string PN, string BN);
    vars(){}
    long long int setMoney(){money = 500;}
    int setXP(){xp = 0;}
    string setPname(){Pname;}
    string setBname(){Bname;}

    long long int getMoney()
    {return money;}
    int getXP()
    {return xp;}
    string getPname()
    {return Pname;}
    string getBname()
    {return Bname;}

    private:
        long long int money;
        int xp;
        string Pname;
        string Bname;
};
Nevermind i did this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class vars
{
    public:
    int game();

    vars(long long int M, int XP, string PN, string BN);
    vars()
    {
        money = 500;
        xp = 0;
        Pname = Pname;
        Bname = Bname;
    }


Is that how im supposed to do it though? it worked but idk.
Topic archived. No new replies allowed.
Pages: 12