What am i doing wrong

I have my code and i cant seem to figure out what im doing wrong?

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <string>
#include <ctime>
#include <random>
#include <fstream>

using namespace std;

class CLASS
{
    public:
        void Player();
        void TRex();
        void Variables();
        CLASS()
        {
            PHealth = 100;
            TRHealth = 100;
        }
    private:
        int PHealth;
        int TRHealth;
};

int main()
{
    CLASS CO;
    CO.Variables();

    string ready;

    cout << "Welcome to the battle arena" << endl;
    cout << "When you are ready please type in 'enter'" << endl;
    cin >> ready;

    if(ready == "Enter" || ready == "enter")
    {
        Player();
    }
    else
    {
        main();
    }
}

void Variables()
{
    int PHealth = 100; //Player Health
    int TRHealth = 100; //T-Rex Health
}

void Player()
{
    string choice;

    cout << "What weapon will you use?" << endl;
    cout << "\n";
    cout << "1) Shotgun = 4 Damage" << endl;
    cout << "2) Pistol = 2 Damage" << endl;
    cout << "3) Knife = 2 Damage" << endl;
    cout << "\n";

    if(choice == "Shotgun" || choice == "shotgun")
    {
        TRHealth -= 4;
    }
    else if(choice == "Pistol" || choice == "pistol")
    {
        TRHealth -= 2;
    }
    else if(choice == "Knife" || choice == "knife")
    {
        TRHealth -= 2;
    }
}

void TRex(int &PHealth, int &TRHealth)
{
    time_t T;
    time(&T);
    srand(T);

    int Time;

    Time = rand() % 5;

    switch(Time)
    {
        case 0:
            cout << "T-Rex used Bite!" << endl;
            cout << "-3 Health" << endl;
            PHealth -= 3;
            break;

        case 1:
            cout << "T-Rex used Chomp!" << endl;
            cout << "-5 Health" << endl;
            PHealth -= 5;
            break;

        case 2:
            cout << "T-Rex used Stomp!" << endl;
            cout << "-2 Health" << endl;
            PHealth -= 2;
            break;
        default:
            cout << "Trex's attack missed!" << endl;
            PHealth -= 0;
    }

    switch(TRHealth)
    {
        case 0:
            cout << "You have won!!" << endl;
            break;
    }

}




C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|38|error: 'Player' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp||In function 'void Variables()':|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|48|warning: unused variable 'PHealth'|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|49|warning: unused variable 'TRHealth'|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp||In function 'void Player()':|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|65|error: 'TRHealth' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|69|error: 'TRHealth' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|73|error: 'TRHealth' was not declared in this scope|
||=== Build finished: 4 errors, 2 warnings ===|
You are not telling that you are working in your class scope
Change the function definations outside the class to these
1
2
3
void CLASS::Variables()
void CLASS::Player()
void CLASS::TRex(int &PHealth, int &TRHealth)
Ok i just got one error now.


C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|38|error: 'Player' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp||In member function 'void CLASS::Variables()':|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|48|warning: unused variable 'PHealth'|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|49|warning: unused variable 'TRHealth'|
||=== Build finished: 1 errors, 2 warnings ===|


Why do i get this?
are you kidding me?
You used class and you use your function like there isnt the class.

YOU MUST use this which told hacker804.

or to put the craracteristics into the class without to put them out.


choice you,if wanna help tell me.
I have no idea what you said. i did what he said,

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <string>
#include <ctime>
#include <random>
#include <fstream>

using namespace std;

class CLASS
{
    public:
        void Player();
        void TRex();
        void Variables();
        CLASS()
        {
            PHealth = 100;
            TRHealth = 100;
        }
    private:
        int PHealth;
        int TRHealth;
};

int main()
{
    string ready;

    cout << "Welcome to the battle arena" << endl;
    cout << "When you are ready please type in 'enter'" << endl;
    cin >> ready;

    if(ready == "Enter" || ready == "enter")
    {
        Player();
    }
    else
    {
        main();
    }
}

void CLASS::Variables()
{
    int PHealth = 100; //Player Health
    int TRHealth = 100; //T-Rex Health
}

void CLASS::Player()
{
    string choice;

    cout << "What weapon will you use?" << endl;
    cout << "\n";
    cout << "1) Shotgun = 4 Damage" << endl;
    cout << "2) Pistol = 2 Damage" << endl;
    cout << "3) Knife = 2 Damage" << endl;
    cout << "\n";

    if(choice == "Shotgun" || choice == "shotgun")
    {
        TRHealth -= 4;
    }
    else if(choice == "Pistol" || choice == "pistol")
    {
        TRHealth -= 2;
    }
    else if(choice == "Knife" || choice == "knife")
    {
        TRHealth -= 2;
    }
}

void CLASS::TRex()
{
    time_t T;
    time(&T);
    srand(T);

    int Time;

    Time = rand() % 5;

    switch(Time)
    {
        case 0:
            cout << "T-Rex used Bite!" << endl;
            cout << "-3 Health" << endl;
            PHealth -= 3;
            break;

        case 1:
            cout << "T-Rex used Chomp!" << endl;
            cout << "-5 Health" << endl;
            PHealth -= 5;
            break;

        case 2:
            cout << "T-Rex used Stomp!" << endl;
            cout << "-2 Health" << endl;
            PHealth -= 2;
            break;
        default:
            cout << "Trex's attack missed!" << endl;
            PHealth -= 0;
    }

    switch(TRHealth)
    {
        case 0:
            cout << "You have won!!" << endl;
            break;
    }

}
Last edited on
Man i will tell it again.

you have two choices.
1)to go into the class and in functions to put {your code}

2)or like you did ,when use them out of class must tell that they are in class so use this.

void <name of class>::<name of function>
{your code}


if didnt understand tell me to give you my skype or just go learn classes
You are not specifying which class is Player() the member function of.

change
Player
to
CO.Player()

Last edited on
MAAAAAAAAAAN GO AND READ ABOUT CLASSES,HACKER804 IS RIGHT.

you are congused with functions and classes.

when we use classes
into the main tell
(name of class) (name of object)

and after
(name of object).(the function)
I got it now
You need to learn properly about classes.I suggest you read through the tutorial again.
one more thing, never call main()

proper way is
1
2
3
4
5
6
7
int main()
{
     //do whatever
     //Just never call main()

    return 0;
}
I would also advise you to read about classes again, you are confusing them with functions..
i could fix the errors but it would be of no use to you.
Topic archived. No new replies allowed.