rpg

why in this program when i press 1 which should subtract 15 from comphealth it doesnt i put cout << comphealth and it still says 250 after i press 1?

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

using namespace std;

int main()
{
    char yourname [20];
    int whatattack;
    int yourhealth = 250;
    int comphealth = 250;

    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;

    cout << yourname << "s turn: ";
    cin >> whatattack;

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

        case 2:
        comphealth - 15;
        break;

        case 3:
        comphealth + 5;
        break;

    }

    cout << comphealth;

    cin.get();
    return 0;

}
Your problem is inside switch statement. You have to write it like variable = variable - n, n being some value. This way you aren't assigning calculated value to the variable. Additionally, your option 3 will heal computer not player, so you need to change that last equation inside switch to add health to yourself. Here is "fixed code":

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    switch(whatattack)
    {
        case 1:
        comphealth = comphealth - 20;
        break;

        case 2:
        comphealth = comphealth - 15;
        break;

        case 3:
        yourhealth = yourhealth + 5;
        break;

    }


S.
Last edited on
thanks
sorry i have another question.im trying to make the program shorter by using funtions and when its my turn it works fine it subtracts 20 if i press 1....but then when its computers turn it subtacts 18 from me but it resets his health back to 250???how do i fix 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <cstdlib>

using namespace std;

void yourchoice()
{
    int comphealth = 250;
    int yourhealth = 250;
    int whatattack;
    char yourname [20];

    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;

}


void compattack()
{
    int comphealth = 250;
    int yourhealth = 250;
    int whatattack;

    cout << "Now its computers turn" << endl;
    cout << "He strikes you in the chest. You lose 18 health! " << endl;

    yourhealth = yourhealth - 18;

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

int main()
{
    char yourname [20];
    int comphealth = 250;
    int yourhealth = 250;
    int whatattack;

    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();

    cin.get();
    return 0;

}
You define a local integer named comphealth inside yourchoice() and compattack(). These are two entirely different variables it is not resetting the health.
Messing around.
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
    #include <iostream>
    #include <windows.h> // Need this for refreshing, this is just for fun so.

    using namespace std;

    void Battle(int &P_HP, int &E_HP, int Action); // only the first two need to be referenced in

    int main()
    {
        int P_HP = 200, E_HP = 225, Action;

        while(true) // infinite loop
        {
            system("cls"); // refresh the screen.
            cout << "\n\n\tGUY -VS- INCREDIBLE FAILER\n";
            cout << "\n\tYour  HP: " << P_HP << "/200";
            cout << "\n\tEnemy HP: " << E_HP << "/225";

            // Check End Conditions:
            // It's here so that the HP etc updates before quitting.
            if(P_HP > 200){ P_HP = 200; }
            if(P_HP < 1){ cout << "\n\n\tYou managed to die.\n\n"; break; }
            if(E_HP > 225){ E_HP = 225; }
            if(E_HP < 1){ cout << "\n\n\tYou killed him.\n\n"; break; }

            cout << "\n\n\tChoose your action:\n\t1) Poke 2) Caress 3) Rest\n\t> ";
            cin >> Action;
            if(Action > 3 || Action < 1) { continue; } // Invalid Input, jump back to start.
            Battle(P_HP, E_HP, Action);
        }
    }

    void Battle(int &P_HP, int &E_HP, int Action)
    {
        switch(Action)
        {
            case 1: // Poke
                // PLAYER STUFF:
                cout << "\n\tYou give the enemy a vicious poking.\n\t\t- Hurts Enemy 70DMG! -\n";
                E_HP -= 70;
                Sleep(2000); // Pauses so player can read text.
                // ENEMY STUFF:
                cout << "\n\tIncredible Failer retaliates with a slap.\n\t\t- Hurts Player 66DMG! -";
                P_HP -= 66;
                Sleep(2000);
                break;
            case 2: // Caress
                // PLAYER STUFF:
                cout << "\n\tYou gently caress your enemy, for some reason.\n\t\t- Heals Enemy 20HP! -\n";
                E_HP += 20;
                Sleep(2000);
                // ENEMY STUFF:
                cout << "\n\tIncredible Failer does not even, what.\n\t\t- Hurts Everyone 35DMG! -";
                P_HP -= 35;
                E_HP -= 35;
                Sleep(2000);
                break;
            case 3: // Rest
                // PLAYER STUFF:
                cout << "\n\tWay to cheat, you glow in a golden aura.\n\t\t- Heals Self 50HP! -\n";
                P_HP += 50;
                Sleep(2000);
                // ENEMY STUFF:
                cout << "\n\tIncredible Failer fails to understand. Oh well.\n\t\t- Hurts Self 10DMG! -";
                E_HP -= 10;
                Sleep(2000);
                break;
        }
    }

You could make MAXHP a const int and do a lot of other things, but you can do that yourself. You could also make it so the enemy uses specific/random attacks depending on what you do, instead of it being "if player does this, enemy does this" like it is in my code. It also doesn't make HP 0 if it goes below 0 but yeah.

EDIT: Actually that end condition is awkard because it 'updates' after they're printed to the screen. You get the gist though.
Last edited on
WhiteWind is a funny guy
ok im haveing problems..i cant make the program end when the ememy or you is at 0 health...help please

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

using namespace std;

void yourchoice()
{
    int comphealth = 175;
    int yourhealth = 175;
    int whatattack;
    char yourname [20];

    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;

}


void compattack()
{
    int comphealth;
    int yourhealth;
    int whatattack;

    cout << "Now its computers turn" << endl;
    cout << "He strikes you in the chest. You lose 18 health! " << endl;

    yourhealth = yourhealth - 18;

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

    if(yourhealth <= 0)
    {
        void exit (int exitcode);
    }
    if(comphealth <= 0)
    {
        void exit (int exitcode);
    }
}

void yourchoiceagain()
{
    int comphealth;
    int yourhealth;
    int whatattack;
    char yourname [20];

    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;

    if(yourhealth <= 0)
    {
        void exit (int exitcode);
    }
    if(comphealth <= 0)
    {
        void exit (int exitcode);
    }

}

int main()
{
    char yourname [20];
    int comphealth = 175;
    int yourhealth = 175;
    int whatattack;

    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;

    if(yourhealth <= 0)
    {
        cout << "You lost!!";
        goto loop;
    }

    else if(comphealth <= 0)
    {
        cout << "You won!!";
        goto loop;
    }

    yourchoice();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();

    loop:

    cin.get();
    return 0;
}
Did you read my post? I modified your code a bit so that you can easily track HP etc, which also includes how to end the program when either you or the enemy dies.

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
    yourchoice();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();
    yourchoiceagain();
    compattack();


Why is this..
Last edited on
Yes I did read your code. I just didn't use it because I don't understand most of it. I apreciate tat you took the time to make it though. I did yourchoiceagain() so many times so the user can have enough turns to kill the computer. Is there a way I could change that on my code with like a loop or something s o when the health is below 0 the game is over
Sorry for the wait, it took me a while to type this up for you. I went into massive detail here, hopefully you'll read it through or else.. haha.

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

    using namespace std;

    // Here is the Battle function. It's the only
    // one you need for the entire battle system.
    // !! FOR NOW, READ THE NOTES IN int main() !!
    void Battle(int &P_HP, int &E_HP, int &Action)
    {
        // Because we sent those variables in from int main(), we can use them
        // in here as if we made them here. You can pretend we're in int main().
        // All of the changes we make in here are kept when we go back to main,
        // because we put the & symbol before the variable names in this function
        // delceration. Read it as "void Battle(int TheVariableFromMain, ...)"

        switch(Action) // If the action you chose was...
        {
            case 1: // ..1 (Sword Attack)
                // WHAT THE PLAYER DOES:
                cout << "\nYou hit the enemy with your sword! (20 Damage)";
                E_HP -= 20; // This is the same as writing "E_HP = E_HP - 20;".
                // WHAT THE ENEMY DOES:
                cout << "\nThe enemy hits you back with his fist! (10 Damage)";
                P_HP -= 10;
                break;
            case 2: // ..2 (Gun Attack)
                // WHAT THE PLAYER DOES:
                cout << "\nYou shoot the enemy with your gun! (12 Damage)";
                E_HP -= 12;
                // WHAT THE ENEMY DOES:
                cout << "\nThe enemy bandages the gunshot wound! (+8 Health)";
                E_HP += 8;
                break;
            case 3: // ..3 (Healing Potion)
                // WHAT THE PLAYER DOES:
                cout << "\nYou quickly drink a healing potion! (+30 Health)";
                P_HP += 30;
                // WHAT THE ENEMY DOES:
                cout << "\nThe enemy heals then quickly attacks! (+10 Health, 6 Damage)";
                E_HP += 10;
                P_HP -= 6;
                break;
        }

        // This is all we need to do in the battle function.
        // We took what the action was, then made the player
        // and the enemy do something, depending on what was
        // chosen. Now, go back to read notes in int main().
    }

    int main()
    {
        // All the variables we need.
        int P_HP = 200, E_HP = 150, Action, Round = 1;

        cout << "\nSimple Battle System\n--------------------"; // "\n" means new line like "endl".

        // This is our main loop which will go around forever until you make it stop.
        // We left the variables outside of this so they don't get set back to their
        // Default values every time it goes round (which means HP would go back to full).
        while(true)
        {
            cout << "\n\nROUND " << Round << ":\n-------"; // display the round.
            cout << "\nPlayer HP: " << P_HP << "/200"; // displays your HP.
            cout << "\nEnemy  HP: " << E_HP << "/150"; // displays enemy HP.
            cout << "\n\nChoose your action:";
            cout << "\n1) Sword Attack, 2) Gun Attack, 3) Healing Potion"; // show the options.
            cout << "\n> ";
            cin >> Action; // choose an action.

            // Here, instead of just calling the battle function, we're going to give it
            // some of the variables we made earlier: P_HP, E_HP and Action. This means
            // that we can use them in the function even though we didn't make them there.
            Battle(P_HP, E_HP, Action);
            // Now read the notes in the Battle() function.

            // Since the Battle() function is in our while loop, the prompt for which action
            // to take and all of the battle stuff will happen over and over, forever. We
            // want it to stop, though, when someone dies (HP reaches 0). We have to add a
            // couple of ifstatements to check if the HP is down to 0.

            if(P_HP < 1){ // if player is dead:
                cout << "\n\n\n---------------------------";
                cout << "\nYou died! Ending Program...";
                cout << "\n---------------------------\n\n";
                break; // breaks out of the while loop, ends the program.
            } else if(E_HP < 1){ // if the enemy is dead:
                cout << "\n\n\n-----------------------------------------";
                cout << "\nYou killed the monster! Ending Program...";
                cout << "\n-----------------------------------------\n\n";
                break; // breaks if this is the case too.
            }

            // If neither the player nor enemy are dead, the program will loop,

            // This means "Round = Round + 1". It's just to
            // show the player what round the player is up to.
            Round++;
        }
    }

.. Here's the same program without all the notes:

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 <iostream>

    using namespace std;

    void Battle(int &P_HP, int &E_HP, int &Action)
    {
        switch(Action)
        {
            case 1:
                cout << "\nYou hit the enemy with your sword! (20 Damage)";
                E_HP -= 20; 
                cout << "\nThe enemy hits you back with his fist! (10 Damage)";
                P_HP -= 10;
                break;
            case 2:
                cout << "\nYou shoot the enemy with your gun! (12 Damage)";
                E_HP -= 12;
                cout << "\nThe enemy bandages the gunshot wound! (+8 Health)";
                E_HP += 8;
                break;
            case 3:
                cout << "\nYou quickly drink a healing potion! (+30 Health)";
                P_HP += 30;
                cout << "\nThe enemy heals then quickly attacks! (+10 Health, 6 Damage)";
                E_HP += 10;
                P_HP -= 6;
                break;
        }
    }

    int main()
    {
        int P_HP = 200, E_HP = 150, Action, Round = 1;

        cout << "\nSimple Battle System\n--------------------";
        
        while(true)
        {
            cout << "\n\nROUND " << Round << ":\n-------";
            cout << "\nPlayer HP: " << P_HP << "/200";
            cout << "\nEnemy  HP: " << E_HP << "/150";
            cout << "\n\nChoose your action:";
            cout << "\n1) Sword Attack, 2) Gun Attack, 3) Healing Potion";
            cout << "\n> ";
            cin >> Action;

            Battle(P_HP, E_HP, Action);

            if(P_HP < 1){
                cout << "\n\n\n---------------------------";
                cout << "\nYou died! Ending Program...";
                cout << "\n---------------------------\n\n";
                break;
            } else if(E_HP < 1){
                cout << "\n\n\n-----------------------------------------";
                cout << "\nYou killed the monster! Ending Program...";
                cout << "\n-----------------------------------------\n\n";
                break;
            }

            Round++;
        }
    }

Things that could be improved:
- If HP goes over the max amount (starting amount), it doesn't go back down to the max.
- If HP goes below the min amount, it doesn't set health to 0 before you die (just a neatness thing).
- The enemy does the same thing every time, so it's kinda boring.
- It doesn't check if what the user inputted was a valid option (eg. choosing 67 for action).
- Lots of other misc. stuff.

Maybe after reading this you'll understand more of my first example.
Last edited on
Topic archived. No new replies allowed.