rpg game

HI tell me what you think about this program. Also how can i put a class in it or is it too small to put a class

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

    int comphealth = 175;
    int yourhealth = 175;
    int whatattack;
    int attack;
    int shoot;
    int heal;
    char yourname [20];
    int restart;

void yourchoice()
{

    srand(time(0));
    attack = rand() %30;
    shoot = rand() %25;
    heal = rand() %11;

    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        comphealth = comphealth - attack;
        break;

        case 2:
        comphealth = comphealth - shoot;
        break;

        case 3:
        yourhealth = yourhealth + heal;
        break;

    }

    cout << "Your health is " << yourhealth << endl;
    cout << "COMPUTERS health is " << comphealth << endl << endl << endl << endl;

}


void compattack()
{
    srand(time(0));
    attack = rand() %40;

    cout << "Now its computers turn" << endl;
    cout << "He strikes you in the chest!" << endl;

    yourhealth = yourhealth - attack;

    cout << "Your health is " << yourhealth << endl;
    cout << "COMPUTERS health is " << comphealth << endl << endl << endl << endl;
}

void yourchoiceagain()
{
    srand(time(0));
    attack = rand() %30;
    shoot = rand() %25;
    heal = rand() %11;

    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        comphealth = comphealth - 20;
        break;

        case 2:
        comphealth = comphealth - 15;
        break;

        case 3:
        yourhealth = yourhealth + 5;
        break;

    }


    cout << "Your health is " << yourhealth << endl;
    cout << "COMPUTERS health is " << comphealth << endl << endl << endl << endl;

}

int main()
{
    do{
    system("CLS");

    if(yourhealth <= 0 || comphealth <= 0){
        yourhealth = 175;
        comphealth = 175;
    }

    cout << "Enter your name: ";
    cin >> yourname;

    cout << yourname << "  VS  " << "COMPUTER" << endl << endl;
    cout << "Here are your options:" << endl;
    cout << "Press 1 and enter to strike your opponent" << endl;
    cout << "Press 2 and enter to shoot your opponent" << endl;
    cout << "Press 3 and enter to heal yourself" << endl << endl;

    cout << "Time to fight! " << endl;

    yourchoice();
    compattack();
    while(comphealth > 0 && yourhealth > 0)
    {
        yourchoiceagain();
        compattack();

        if(comphealth <= 0 || yourhealth <= 0){
            break;
        }
    }

    if(comphealth <= 0){
        cout << "You win!" << endl;
    }
    else if(yourhealth <= 0){
        cout << "You lose" << endl;
    }

    cout << "Enter 1 to quit or enter 2 to restart: ";
    cin >> restart;

    }while(restart == 2);

    cin.get();
    return 0;
}
Last edited on
First of all, you need to use function prototypes. It can cause all kinds of "undeclared function" errors if you call a function somewhere that is declared further down in the program.

Second... nothing is too small for a class, but judging by your program's look and simplicity, I'd say you're probably not ready for classes. Learn the basics, first.
Last edited on
what are function prototypes
and if i did know enough for classes...how would i use them in this program
function prototypes is the same as function declarations. You don't really need it here because you define the function before you call it.

You should only call srand once at the start of your program. Make the call at the start of main and remove all other calls to srand.
i wrote this program to prove to you i know how to make classes but can you please tell me how i would use a class in the above program?

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

using namespace std;

class howmuchpay
{
    public:

    double x;
    double y;


};


int main()
{
    howmuchpay employee1;
    double pay;

    cout << "PAY CALCULATOR" << endl << endl;
    cout << "How many hours has your employee worked: ";
    cin >> employee1.x;
    cout << "How much do you pay your employee per hour: ";
    cin >> employee1.y;

    pay = employee1.x * employee1.y;

    cout << pay;


    cin.get();
    return 0;
}
Classes go far, far beyond that little tiny simplistic view of it. You can actually make your player and the computer each into a subclass of a character, one controllable by the computer, and the other controllable by the user. Then you make functions that interact with their data to repetitively enter a function that can access both of the subclasses and perform the combat functions.
how do i do that?
That's what packetpirate was implying. If you don't know how to use methods and interact between classes, then you need to study up a bit more.
ok ill study but can you help me with this.when i do 2 player shoot doesnt work and also random doesnt work we both deal the same amount of damage with #1 attack?why?
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

    int comphealth = 175;
    int yourhealth = 175;
    int whatattack;
    int attack;
    int shoot;
    int heal;
    char yourname [20];
    char p2name [20];
    int restart;
    int howmanyplayers;
    int p2health = 175;

void yourchoice()
{

    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        comphealth = comphealth - attack;
        break;

        case 2:
        comphealth = comphealth - shoot;
        break;

        case 3:
        yourhealth = yourhealth + heal;
        break;

    }

    cout << "Your health is " << yourhealth << endl;
    cout << "COMPUTERS health is " << comphealth << endl << endl << endl << endl;

}


void compattack()
{
    srand(time(0));
    attack = rand() %40;

    cout << "Now its computers turn" << endl;
    cout << "He strikes you in the chest!" << endl;

    yourhealth = yourhealth - attack;

    cout << "Your health is " << yourhealth << endl;
    cout << "COMPUTERS health is " << comphealth << endl << endl << endl << endl;
}

void p1attack()
{
    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        p2health = p2health - attack;
        break;

        case 2:
        p2health = p2health - shoot;
        break;

        case 3:
        yourhealth = yourhealth + heal;
        break;

    }

    cout << "Player 1s health is " << yourhealth << endl;
    cout << "Player 2s health is " << p2health << endl << endl << endl << endl;

}
void p2attack()
{
    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        yourhealth = yourhealth - attack;
        break;

        case 2:
        yourhealth = yourhealth - shoot;
        break;

        case 3:
        p2health = p2health + heal;
        break;

    }

    cout << "Player 1s health is " << yourhealth << endl;
    cout << "Player 2s health is " << p2health << endl << endl << endl << endl;

}


int main()
{
    do{
    system("CLS");

    cout << "Enter 1 for 1 player or 2 for 2 player: ";
    cin >> howmanyplayers;

    switch(howmanyplayers){
    case 1:

    srand(time(0));
    attack = rand() %30;
    shoot = rand() %25;
    heal = rand() %11;

    if(yourhealth <= 0 || comphealth <= 0){
        yourhealth = 175;
        comphealth = 175;
    }

    cout << "Enter your name: ";
    cin >> yourname;

    cout << yourname << "  VS  " << "COMPUTER" << endl << endl;
    cout << "Here are your options:" << endl;
    cout << "Press 1 and enter to strike your opponent" << endl;
    cout << "Press 2 and enter to shoot your opponent" << endl;
    cout << "Press 3 and enter to heal yourself" << endl << endl;

    cout << "Time to fight! " << endl;

    while(comphealth > 0 && yourhealth > 0)
    {
        yourchoice();
        compattack();

        if(comphealth <= 0 || yourhealth <= 0){
            break;
        }
    }

    if(comphealth <= 0){
        cout << "You win!" << endl;
    }
    else if(yourhealth <= 0){
        cout << "You lose" << endl;
    }

    cout << "Enter 1 to quit or enter 2 to restart: ";
    cin >> restart;

    break;

    case 2:

    srand(time(0));
    attack = rand() %30;
    shoot = rand() %25;
    heal = rand() %11;

    if(yourhealth <= 0 || p2health <= 0){
        yourhealth = 175;
        p2health = 175;
    }

    cout << "Enter player ones name: ";
    cin >> yourname;
    cout << endl << endl;
    cout << "Enter player twos name: ";
    cin >> p2name;
    cout << endl << endl;


    cout << yourname << "  VS  " << p2name << endl << endl;
    cout << "Here are your options:" << endl;
    cout << "Press 1 and enter to strike your opponent" << endl;
    cout << "Press 2 and enter to shoot your opponent" << endl;
    cout << "Press 3 and enter to heal yourself" << endl << endl;

    cout << "Time to fight! " << endl;

    while(p2health > 0 && yourhealth > 0)
    {
        p1attack();
        p2attack();

        if(p2health <= 0 || yourhealth <= 0){
            break;
        }
    }

    if(p2health <= 0){
        cout << yourname << " wins!!!" << endl;
    }
    else if(yourhealth <= 0){
        cout << p2name << " wins!!" << endl;
    }
    break;
    }

    cout << "Enter 1 to quit or enter 2 to restart: ";
    cin >> restart;

    }while(restart == 2);

    cin.get();
    return 0;
}


Well, that's simple. You're only using the random once, and then passing the same value to both attack functions.
ok i found a way to use classes in this program.but is there a way i can put this function into class player1xx1player and then call it in 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
void yourchoice()
{

    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        comphealth = comphealth - attack;
        break;

        case 2:
        comphealth = comphealth - shoot;
        break;

        case 3:
        yourhealth = yourhealth + heal;
        break;

    }

    cout << "Your health is " << yourhealth << endl;
    cout << "COMPUTERS health is " << comphealth << endl << endl << endl << endl;

}


heres the full 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

    int comphealth = 175;
    int yourhealth = 175;
    int whatattack;
    int attack;
    int shoot;
    int heal;
    int restart;
    int howmanyplayers;
    int p2health = 175;


    class player1xx1player
    {
        public:
        char yourname [20];
    };

    class player1xx2player
    {
        public:
        char p1name [20];
    };

    class player2xx2player
    {
        public:
        char p2name [20];
    };


void yourchoice()
{

    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        comphealth = comphealth - attack;
        break;

        case 2:
        comphealth = comphealth - shoot;
        break;

        case 3:
        yourhealth = yourhealth + heal;
        break;

    }

    cout << "Your health is " << yourhealth << endl;
    cout << "COMPUTERS health is " << comphealth << endl << endl << endl << endl;

}


void compattack()
{
    srand(time(0));
    attack = rand() %40;

    cout << "Now its computers turn" << endl;
    cout << "He strikes you in the chest!" << endl;

    yourhealth = yourhealth - attack;

    cout << "Your health is " << yourhealth << endl;
    cout << "COMPUTERS health is " << comphealth << endl << endl << endl << endl;
}

void p1attack()
{
    srand(time(0));
    attack = rand() %30;
    shoot = rand() %25;
    heal = rand() %11;

    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        p2health = p2health - attack;
        break;

        case 2:
        p2health = p2health - shoot;
        break;

        case 3:
        yourhealth = yourhealth + heal;
        break;

    }

    cout << "Player 1s health is " << yourhealth << endl;
    cout << "Player 2s health is " << p2health << endl << endl << endl << endl;

}
void p2attack()
{
    srand(time(0));
    attack = rand() %30;
    shoot = rand() %25;
    heal = rand() %11;

    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        yourhealth = yourhealth - attack;
        break;

        case 2:
        yourhealth = yourhealth - shoot;
        break;

        case 3:
        p2health = p2health + heal;
        break;

    }

    cout << "Player 1s health is " << yourhealth << endl;
    cout << "Player 2s health is " << p2health << endl << endl << endl << endl;

}


int main()
{
    do{
    system("CLS");

    cout << "Enter 1 for 1 player or 2 for 2 player: ";
    cin >> howmanyplayers;

    switch(howmanyplayers){
    case 1:

    player1xx1player only1p;

    if(yourhealth <= 0 || comphealth <= 0){
        yourhealth = 175;
        comphealth = 175;
    }

    cout << "Enter your name: ";
    cin >> only1p.yourname;

    cout << only1p.yourname << "  VS  " << "COMPUTER" << endl << endl;
    cout << "Here are your options:" << endl;
    cout << "Press 1 and enter to strike your opponent" << endl;
    cout << "Press 2 and enter to shoot your opponent" << endl;
    cout << "Press 3 and enter to heal yourself" << endl << endl;

    cout << "Time to fight! " << endl;

    while(comphealth > 0 && yourhealth > 0)
    {
        yourchoice();
        if(comphealth <= 0 || yourhealth <= 0){
            break;
        compattack();

        if(comphealth <= 0 || yourhealth <= 0){
            break;
        }
    }

    if(comphealth <= 0){
        cout << "You win!" << endl;
    }
    else if(yourhealth <= 0){
        cout << "You lose" << endl;
    }

    cout << "Enter 1 to quit or enter 2 to restart: ";
    cin >> restart;

    break;

    case 2:

    player1xx2player p1;
    player2xx2player p2;

    srand(time(0));
    attack = rand() %30;
    shoot = rand() %25;
    heal = rand() %11;

    if(yourhealth <= 0 || p2health <= 0){
        yourhealth = 175;
        p2health = 175;
    }

    cout << "Enter player ones name: ";
    cin >> p1.p1name;
    cout << endl << endl;
    cout << "Enter player twos name: ";
    cin >> p2.p2name;
    cout << endl << endl;


    cout << p1.p1name << "  VS  " << p2.p2name << endl << endl;
    cout << "Here are your options:" << endl;
    cout << "Press 1 and enter to strike your opponent" << endl;
    cout << "Press 2 and enter to shoot your opponent" << endl;
    cout << "Press 3 and enter to heal yourself" << endl << endl;

    cout << "Time to fight! " << endl;

    while(p2health > 0 && yourhealth > 0)
    {
        p1attack();
        if(p2health <= 0 || yourhealth <= 0){
            break;
        }
        p2attack();

        if(p2health <= 0 || yourhealth <= 0){
            break;
        }
    }

    if(p2health <= 0){
        cout << p1.p1name << " wins!!!" << endl;
    }
    else if(yourhealth <= 0){
        cout << p2.p2name << " wins!!" << endl;
    }
    break;
    }

    cout << "Enter 1 to quit or enter 2 to restart: ";
    cin >> restart;
    }
    }while(restart == 2);

    cin.get();
    return 0;
}
never mind i figured it out..its called a member function...but i got an error...why?

heres the error
|234|error: expected unqualified-id before '.' token|

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

    int comphealth = 175;
    int yourhealth = 175;
    int whatattack;
    int attack;
    int shoot;
    int heal;
    int restart;
    int howmanyplayers;
    int p2health = 175;
    int whatcomputerattack;


    class player1xx1player
    {
        public:
        char yourname [20];
    };

    class player1xx2player
    {
        public:
        char p1name [20];

        void p1attack()
{
    srand(time(0));
    attack = rand() %30;
    shoot = rand() %25;
    heal = rand() %11;

    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        p2health = p2health - attack;
        break;

        case 2:
        p2health = p2health - shoot;
        break;

        case 3:
        yourhealth = yourhealth + heal;
        break;

    }

    cout << "Player 1s health is " << yourhealth << endl;
    cout << "Player 2s health is " << p2health << endl << endl << endl << endl;

}

    };

    class player2xx2player
    {
        public:
        char p2name [20];
    };


void yourchoice()
{

    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        comphealth = comphealth - attack;
        break;

        case 2:
        comphealth = comphealth - shoot;
        break;

        case 3:
        yourhealth = yourhealth + heal;
        break;

    }

    cout << "Your health is " << yourhealth << endl;
    cout << "COMPUTERS health is " << comphealth << endl << endl << endl << endl;

}


void compattack()
{
    srand(time(0));
    attack = rand() %40;

    cout << "Now its computers turn" << endl << endl;

    yourhealth = yourhealth - attack;


    cout << "Your health is " << yourhealth << endl;
    cout << "COMPUTERS health is " << comphealth << endl << endl << endl << endl;
}


void p2attack()
{
    srand(time(0));
    attack = rand() %30;
    shoot = rand() %25;
    heal = rand() %11;

    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        yourhealth = yourhealth - attack;
        break;

        case 2:
        yourhealth = yourhealth - shoot;
        break;

        case 3:
        p2health = p2health + heal;
        break;

    }

    cout << "Player 1s health is " << yourhealth << endl;
    cout << "Player 2s health is " << p2health << endl << endl << endl << endl;

}


int main()
{
    do{
    system("CLS");

    cout << "Enter 1 for 1 player or 2 for 2 player: ";
    cin >> howmanyplayers;

    switch(howmanyplayers){
    case 1:

    player1xx1player only1p;

    if(yourhealth <= 0 || comphealth <= 0){
        yourhealth = 175;
        comphealth = 175;
    }

    cout << "Enter your name: ";
    cin >> only1p.yourname;

    cout << only1p.yourname << "  VS  " << "COMPUTER" << endl << endl;
    cout << "Here are your options:" << endl;
    cout << "Press 1 and enter to strike your opponent" << endl;
    cout << "Press 2 and enter to shoot your opponent" << endl;
    cout << "Press 3 and enter to heal yourself" << endl << endl;

    cout << "Time to fight! " << endl;

    while(comphealth > 0 && yourhealth > 0)
    {
        yourchoice();
        if(comphealth <= 0 || yourhealth <= 0){
            break;
        }

        compattack();

        if(comphealth <= 0 || yourhealth <= 0){
            break;
        }



    if(comphealth <= 0){
        cout << "You win!" << endl;
    }
    else if(yourhealth <= 0){
        cout << "You lose" << endl;
    }

    cout << "Enter 1 to quit or enter 2 to restart: ";
    cin >> restart;

    break;

    case 2:

    player1xx2player p1;
    player2xx2player p2;

    srand(time(0));
    attack = rand() %30;
    shoot = rand() %25;
    heal = rand() %11;

    if(yourhealth <= 0 || p2health <= 0){
        yourhealth = 175;
        p2health = 175;
    }

    cout << "Enter player ones name: ";
    cin >> p1.p1name;
    cout << endl << endl;
    cout << "Enter player twos name: ";
    cin >> p2.p2name;
    cout << endl << endl;


    cout << p1.p1name << "  VS  " << p2.p2name << endl << endl;
    cout << "Here are your options:" << endl;
    cout << "Press 1 and enter to strike your opponent" << endl;
    cout << "Press 2 and enter to shoot your opponent" << endl;
    cout << "Press 3 and enter to heal yourself" << endl << endl;

    cout << "Time to fight! " << endl;

    while(p2health > 0 && yourhealth > 0)
    {
        player1xx2player.p1attack();

        if(p2health <= 0 || yourhealth <= 0){
            break;
        }
        p2attack();

        if(p2health <= 0 || yourhealth <= 0){
            break;
        }
    }

    if(p2health <= 0){
        cout << p1.p1name << " wins!!!" << endl;
    }
    else if(yourhealth <= 0){
        cout << p2.p2name << " wins!!" << endl;
    }
    break;
    }

    cout << "Enter 1 to quit or enter 2 to restart: ";
    cin >> restart;
    }
    }while(restart == 2);

    cin.get();
    return 0;
}
please answer
Here is the working code (sorry that I couldn't get it in earlier, I suddenly had to get something done just before I'd finished working with the 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

    int comphealth = 175;
    int yourhealth = 175;
    int whatattack;
    int attack;
    int shoot;
    int heal;
    int restart;
    int howmanyplayers;
    int p2health = 175;


    class player1xx1player
    {
        public:
        char yourname [20];
        void yourchoice()
{

    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        comphealth = comphealth - attack;
        break;

        case 2:
        comphealth = comphealth - shoot;
        break;

        case 3:
        yourhealth = yourhealth + heal;
        break;

    }

    cout << "Your health is " << yourhealth << endl;
    cout << "COMPUTERS health is " << comphealth << endl << endl << endl << endl;

}
    };

    class player1xx2player
    {
        public:
        char p1name [20];
    };

    class player2xx2player
    {
        public:
        char p2name [20];
    };


void compattack()
{
    srand(time(0));
    attack = rand() %40;

    cout << "Now its computers turn" << endl;
    cout << "He strikes you in the chest!" << endl;

    yourhealth = yourhealth - attack;

    cout << "Your health is " << yourhealth << endl;
    cout << "COMPUTERS health is " << comphealth << endl << endl << endl << endl;
}

void p1attack()
{
    srand(time(0));
    attack = rand() %30;
    shoot = rand() %25;
    heal = rand() %11;

    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        p2health = p2health - attack;
        break;

        case 2:
        p2health = p2health - shoot;
        break;

        case 3:
        yourhealth = yourhealth + heal;
        break;

    }

    cout << "Player 1s health is " << yourhealth << endl;
    cout << "Player 2s health is " << p2health << endl << endl << endl << endl;

}
void p2attack()
{
    srand(time(0));
    attack = rand() %30;
    shoot = rand() %25;
    heal = rand() %11;

    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        yourhealth = yourhealth - attack;
        break;

        case 2:
        yourhealth = yourhealth - shoot;
        break;

        case 3:
        p2health = p2health + heal;
        break;

    }

    cout << "Player 1s health is " << yourhealth << endl;
    cout << "Player 2s health is " << p2health << endl << endl << endl << endl;

}


int main()
{
    do{
    system("CLS");

    cout << "Enter 1 for 1 player or 2 for 2 player: ";
    cin >> howmanyplayers;

    switch(howmanyplayers){
    case 1:

    player1xx1player only1p;

    if(yourhealth <= 0 || comphealth <= 0){
        yourhealth = 175;
        comphealth = 175;
    }

    cout << "Enter your name: ";
    cin >> only1p.yourname;

    cout << only1p.yourname << "  VS  " << "COMPUTER" << endl << endl;
    cout << "Here are your options:" << endl;
    cout << "Press 1 and enter to strike your opponent" << endl;
    cout << "Press 2 and enter to shoot your opponent" << endl;
    cout << "Press 3 and enter to heal yourself" << endl << endl;

    cout << "Time to fight! " << endl;

    while(comphealth > 0 && yourhealth > 0)
    {
        only1p.yourchoice();
        if(comphealth <= 0 || yourhealth <= 0){
            break;
        compattack();

        if(comphealth <= 0 || yourhealth <= 0){
            break;
        }
    }

    if(comphealth <= 0){
        cout << "You win!" << endl;
    }
    else if(yourhealth <= 0){
        cout << "You lose" << endl;
    }

    cout << "Enter 1 to quit or enter 2 to restart: ";
    cin >> restart;

    break;

    case 2:

    player1xx2player p1;
    player2xx2player p2;

    srand(time(0));
    attack = rand() %30;
    shoot = rand() %25;
    heal = rand() %11;

    if(yourhealth <= 0 || p2health <= 0){
        yourhealth = 175;
        p2health = 175;
    }

    cout << "Enter player ones name: ";
    cin >> p1.p1name;
    cout << endl << endl;
    cout << "Enter player twos name: ";
    cin >> p2.p2name;
    cout << endl << endl;


    cout << p1.p1name << "  VS  " << p2.p2name << endl << endl;
    cout << "Here are your options:" << endl;
    cout << "Press 1 and enter to strike your opponent" << endl;
    cout << "Press 2 and enter to shoot your opponent" << endl;
    cout << "Press 3 and enter to heal yourself" << endl << endl;

    cout << "Time to fight! " << endl;

    while(p2health > 0 && yourhealth > 0)
    {
        p1attack();
        if(p2health <= 0 || yourhealth <= 0){
            break;
        }
        p2attack();

        if(p2health <= 0 || yourhealth <= 0){
            break;
        }
    }

    if(p2health <= 0){
        cout << p1.p1name << " wins!!!" << endl;
    }
    else if(yourhealth <= 0){
        cout << p2.p2name << " wins!!" << endl;
    }
    break;
    }

    cout << "Enter 1 to quit or enter 2 to restart: ";
    cin >> restart;
    }
    }while(restart == 2);

    cin.get();
    return 0;
}


Basically, you just move the "yourchoice" function to the inside of the class and replace line 170 with only1p.yourchoice();. I hope that this helps.
do you know why 1 player isnt working?
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

    int comphealth = 175;
    int yourhealth = 175;
    int whatattack;
    int attack;
    int shoot;
    int heal;
    int restart;
    int howmanyplayers;
    int p2health = 175;


    class player1xx1player
    {
        public:
        char yourname [20];

void yourchoice()
{

    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        comphealth = comphealth - attack;
        break;

        case 2:
        comphealth = comphealth - shoot;
        break;

        case 3:
        yourhealth = yourhealth + heal;
        break;

    }

    cout << "Your health is " << yourhealth << endl;
    cout << "COMPUTERS health is " << comphealth << endl << endl << endl << endl;

}
    };

    class player1xx2player
    {
        public:
        char p1name [20];

void p1attack()
{
    srand(time(0));
    attack = rand() %30;
    shoot = rand() %25;
    heal = rand() %11;

    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        p2health = p2health - attack;
        break;

        case 2:
        p2health = p2health - shoot;
        break;

        case 3:
        yourhealth = yourhealth + heal;
        break;

    }

    cout << "Player 1s health is " << yourhealth << endl;
    cout << "Player 2s health is " << p2health << endl << endl << endl << endl;

}

    };

    class player2xx2player
    {
        public:
        char p2name [20];

void p2attack()
{
    srand(time(0));
    attack = rand() %30;
    shoot = rand() %25;
    heal = rand() %11;

    cout << "Its your turn: ";
    cin >> whatattack;

    switch(whatattack)
    {
        case 1:
        yourhealth = yourhealth - attack;
        break;

        case 2:
        yourhealth = yourhealth - shoot;
        break;

        case 3:
        p2health = p2health + heal;
        break;

    }

    cout << "Player 1s health is " << yourhealth << endl;
    cout << "Player 2s health is " << p2health << endl << endl << endl << endl;

}

    };


void compattack()
{
    srand(time(0));
    attack = rand() %40;

    cout << "Now its computers turn" << endl;
    cout << "He strikes you in the chest!" << endl;

    yourhealth = yourhealth - attack;

    cout << "Your health is " << yourhealth << endl;
    cout << "COMPUTERS health is " << comphealth << endl << endl << endl << endl;
}





int main()
{
    do{
    system("CLS");

    cout << "Enter 1 for 1 player or 2 for 2 player: ";
    cin >> howmanyplayers;

    switch(howmanyplayers){
    case 1:

    player1xx1player only1p;

    if(yourhealth <= 0 || comphealth <= 0){
        yourhealth = 175;
        comphealth = 175;
    }

    cout << "Enter your name: ";
    cin >> only1p.yourname;

    cout << only1p.yourname << "  VS  " << "COMPUTER" << endl << endl;
    cout << "Here are your options:" << endl;
    cout << "Press 1 and enter to strike your opponent" << endl;
    cout << "Press 2 and enter to shoot your opponent" << endl;
    cout << "Press 3 and enter to heal yourself" << endl << endl;

    cout << "Time to fight! " << endl;

    while(comphealth > 0 && yourhealth > 0)
    {
        only1p.yourchoice();
        if(comphealth <= 0 || yourhealth <= 0){
            break;
        compattack();

        if(comphealth <= 0 || yourhealth <= 0){
            break;
        }
    }

    if(comphealth <= 0){
        cout << "You win!" << endl;
    }
    else if(yourhealth <= 0){
        cout << "You lose" << endl;
    }

    cout << "Enter 1 to quit or enter 2 to restart: ";
    cin >> restart;

    break;

    case 2:

    player1xx2player p1;
    player2xx2player p2;

    srand(time(0));
    attack = rand() %30;
    shoot = rand() %25;
    heal = rand() %11;

    if(yourhealth <= 0 || p2health <= 0){
        yourhealth = 175;
        p2health = 175;
    }

    cout << "Enter player ones name: ";
    cin >> p1.p1name;
    cout << endl << endl;
    cout << "Enter player twos name: ";
    cin >> p2.p2name;
    cout << endl << endl;


    cout << p1.p1name << "  VS  " << p2.p2name << endl << endl;
    cout << "Here are your options:" << endl;
    cout << "Press 1 and enter to strike your opponent" << endl;
    cout << "Press 2 and enter to shoot your opponent" << endl;
    cout << "Press 3 and enter to heal yourself" << endl << endl;

    cout << "Time to fight! " << endl;

    while(p2health > 0 && yourhealth > 0)
    {
        p1.p1attack();
        if(p2health <= 0 || yourhealth <= 0){
            break;
        }
        p2.p2attack();

        if(p2health <= 0 || yourhealth <= 0){
            break;
        }
    }

    if(p2health <= 0){
        cout << p1.p1name << " wins!!!" << endl;
    }
    else if(yourhealth <= 0){
        cout << p2.p2name << " wins!!" << endl;
    }
    break;
    }

    cout << "Enter 1 to quit or enter 2 to restart: ";
    cin >> restart;
    }
    }while(restart == 2);

    cin.get();
    return 0;
}
Topic archived. No new replies allowed.