Combat system trouble

I am learning the concepts of OOP, and I decided to try to make a simple combat system. I am trying to see if what I have done is working and it is not recognizing the variable I am testing. The error occurs on line 31. Help would be appreciated.

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
 #include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;


class Combat
{
public:
    int mobSelector(int mob);
    int damage(int health);
    int fight();
private:
    int mob;
    int health;

};

int Combat::mobSelector(int enamy)
{
    enamy = rand()%5+1;
    return enamy;
}

int main()
{
   srand(time(NULL));
    Combat monster;

    switch (monster.mobSelector(mob))
    {
    case 1:
        cout << "It works" << endl;
        break;
    default:
        cout<< "Still works" << endl;
    }
}
Last edited on
In main, you sporadically introduce mob; where is it defined in this scope!?

Edit: you make so many mistakes in this program that I hardly believe it'll end up working.

Here's my attempt at trying to salvage your code:

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
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;


class Combat
{
public:
    int mobSelector();
    int damage(int);
    int fight();
private:
    int mob;
    int health;

};

int Combat::mobSelector()
{
    return rand() % 5 + 1;
}

int main()
{
    srand(time(NULL));

    Combat monster;

    switch (monster.mobSelector())
    {
        case 1:
            cout << "It works" << endl;
            break;
        default:
            cout<< "Still works" << endl;
    }

    return 0;
}
Last edited on
closed account (28poGNh0)
mob is a data member ,you can only use it by calling the class object(monster in your case) like this monster.mob

also the mob is a private you cannot work with it outside the member functions otherwise you should turn it to a public one

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
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;


class Combat
{
    public:
        int mob;

        int mobSelector(int mob);
        int damage(int health);
        int fight();

    private:
        int health;
};

int Combat::mobSelector(int enamy)
{
    enamy = rand()%5+1;
    return enamy;
}

int main()
{
   srand(time(NULL));
    Combat monster;

    switch (monster.mobSelector(monster.mob))
    {
    case 1:
        cout << "It works" << endl;
        break;
    default:
        cout<< "Still works" << endl;
    }
}

Last edited on
Of course, your function doesn't use the parameter in any way, so it's irrelevant what you put there.
Also, it doesn't matter which object calls the method, as you don't care for state.
Well Josue Molina, it is working. My main problem was that I wasn't using an object to access mob, and of course I had accidentally put it in private, thanks for your help.
Topic archived. No new replies allowed.