Separate Class variable returns as 0.

Hey, I was messing around and thought I would try and build a text based RPG to test the things I've learned. I've encountered an issue with a public variable in one of my classes - it works in the class as it's supposed to, but when I try and call it from main it returns as 0 (rather than what it was set to). Probably an easy fix, but I just can't seem to find the issue.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include "charselect.h"
#include "warrior.h"
#include "rogue.h"
#include "mage.h"
#include "exp.h"
#include "mm.h"
#include <string>

using namespace std;
int quit();
warrior war;
mage mg;
rogue rg;
charselect cs;

int main()
{
    cout << "This is a text based RPG - Work in progress." << endl;
    cout << "This game is for learning purposes, and should not be taken seriously. \n\n";
    mm menu;
    switch(cs.x)
        {
        case 1:
            {
                cout << cs.x << "<---- value of x\n\n";
                war.warstart();
                break;
            }
        case 2:
            {
                cout << cs.x << "<---- value of x\n\n";
                mg.magestart();
                break;
            }
        case 3:
            {
                cout << cs.x << "<---- value of x\n\n";
                rg.roguestart();
                break;
            }
        default:
            {
                cout << cs.x << "<---- value of x\n\n";
                break;
            }
        }
    cout << war.getstr() << endl;
    cout << war.getvit() << endl;
    cout << war.getdex() << endl;
    cout << war.getwis() << endl;
    cout << war.getintl() << endl;
    cout << war.getlvl() << endl;
    war.levelup();
    cout << war.getstr() << endl;
    cout << war.getvit() << endl;
    cout << war.getdex() << endl;
    cout << war.getwis() << endl;
    cout << war.getintl() << endl;
    cout << war.getlvl() << endl;



    return 0;




}

int quit()
{
    return 0;
}


mm header :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef MM_H
#define MM_H
#include <iostream>

using namespace std;

class mm
{
    public:
        mm();
        int casecheck();
    protected:
    private:
};

#endif // MM_H 


mm.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
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 "mm.h"
#include <iostream>
#include "charselect.h"
#include "exp.h"
#include <iostream>




mm::mm()
{

    cout << "Please choose one of the following options :\n\n";
    cout << "1 for new game.\n";
    cout << "2 for load game.\n";
    cout << "3 to exit program.\n\n";
    casecheck();
}
int mm::casecheck()
    {
        int mc;

        cin >> mc;
            switch(mc)
        {
            case 1:
            {
                cout << "-------------------------------------------------------------------------------- \n\n";
                charselect cs;
                cs.setchar();
                break;
            }
            case 2:
            {
                cout << "Currently this program does not support a save/load feature.\n\n";
                mm();
                break;
            }
            case 3:
            {
                //supposed to be an exit code, but leaving it as is atm to work on other things.
                return 0;
                break;
            }
            default:
            {
                mm();
            }
        }
    }


charselect header -
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef CHARSELECT_H
#define CHARSELECT_H
#include "mage.h"
#include "rogue.h"
#include "warrior.h"
#include <iostream>

using namespace std;

class charselect
{
    public:
        charselect();
        int setchar();
        int x;
    protected:
    private:

};

#endif // CHARSELECT_H 


charselect.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
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
61
62
63
#include "charselect.h"
#include "warrior.h"
#include "mage.h"
#include "rogue.h"
#include <iostream>
#include <string>

using namespace std;

string role;

charselect::charselect()
{

}

int charselect::setchar()
{
                cout << "We will begin by choosing what class of character you want to play.\n";
                cout << "There are currently 3 classes to pick from : warrior, mage, and rogue.\n\n";
                cout << "The warrior is a melee fighter who relies on his large amount of\n";
                cout << "vitality and strength to defeat his foes.\n\n";
                cout << "The mage is a spell caster who relies on his intelligence to\n";
                cout << "quickly decimate enemy forces\n\n";
                cout << "The assassin is a cunning fighter who utilizes strength and dexterity to.\n";
                cout << "slice through enemy ranks.\n\n";
                cout << "-------------------------------------------------------------------------------- \n\n";
                cout << "Please choose the number of the class you would like to play :\n\n";
                cout << "1. warrior\n";
                cout << "2. mage\n";
                cout << "3. rogue\n\n";
                cin >> x;
                switch(x)
                    {
                        case 1:
                            {
                                role = "warrior. \n";
                                x = 1;
                                break;
                            }
                        case 2:
                            {
                                role = "mage.\n";
                                x = 2;
                                break;
                            }
                        case 3:
                            {
                                role = "rogue. \n";
                                x = 3;
                                break;
                            }
                        default :
                            {
                            while (x < 1 || x > 3)
                                {
                                    cout << "Please re-enter your class.\n";
                                    cin >> x;
                                }
                    }
                cout << "You have chosen the role of : " << role << endl;
    }
}


Now if you look at main - lines 22 - 47, you can see the switch I made, which is supposed to use the x variable used to choose a character in the charselect class. However, every time I try to get that variable it comes back as 0 (where I need it to retain the value it had when the character select was made).
BUT ... You don't do any character selection.....
I do not understand what value of x do you expect shall be?! After the statement charselect cs; data member x is not initialized.
Last edited on
thanks vlad, that solved it.
Topic archived. No new replies allowed.