Need help!

oddly, after a battle with any monster including after changing the hp, strength, etc, your health always goes down to 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
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
   #include <iostream>

#include <string>

using namespace std;
	
class player
{

public:

string name;

int hp;

int maxhp;

int food;

int hunger;

int selection;

int defense;

int strength;

};

class monster
{
public:

	int hp;

	int defense;

	int strength;

};

int main()
{

	system("title Adventure!");

	Dead:

	system("CLS");

	player player;

	int day = 0;

	player.defense = 3;

	player.hp = 100;
		
	player.food = 0;
	
	player.hunger = 0;

	player.maxhp = 100;

	player.strength = 10;

	cout << "Who are you?" << endl;

	cin >> player.name;

	system("CLS");
	
	cout << "Ok " << player.name << ", you have awoken deep in a forest, you remember nothing except your name." << endl;
	
	system("Pause");
	
	system("CLS");
	
	while(true){

		cout << "Day: " << day << endl << "Food: " << player.food << endl << "Health: " << player.hp << endl << "Hunger: " << player.hunger << "/35" << endl << "What shall you do today?" << endl << "1.Forage for food." << endl << "2.Nothing." << endl << "3.Explore." << endl;
		
		cin >> player.selection;
		
		system("CLS");

		if(player.selection == 1) player.food = player.food + 2;

		if((player.selection == 2)&&((player.hp < player.maxhp)&&(player.food > 0)))player.hp++;
		
		if(player.selection == 3)
		{

			monster monster;

			monster.hp = 100;

			monster.strength = 3;
			
			monster.defense = 2;

			cout << "You encounter a monster!" << endl << "Enemy HP: " << monster.hp << endl << "Your AP: " << player.strength << endl << "your HP: "  << player.hp << endl << "1. Fight." << endl << "2. Run." << endl;

			cin >> player.selection;
			
			if(player.selection == 1)
			{
				while((player.hp > 0)||(monster.hp > 0))
				{
					
					if(player.defense < monster.strength) player.hp = player.hp - (monster.strength - player.defense);
					if(monster.defense < player.strength) monster.hp = monster.hp - (player.strength - monster.defense);
				
				}
			}
			system("CLS");
		}	
Not sure if this is the only problem, but I believe you want
/*Line 108*/ while((player.hp > 0) && (monster.hp > 0)).

Having it only be OR will make it still continue if player.hp is positive and monster.hp is negative, or vice-versa. Using the debugger or having cout statements inside the while loop helps.
Last edited on
This is your program logic. Run it to see what's 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
#include <iostream>
#include <iomanip>

using namespace std;

struct Person
{
        int hp, defense, strength;
};

int main()
{
        Person player;
        player.hp = 100;
        player.defense = 3;
        player.strength = 10;

        Person monster;
        monster.hp = 100;
        monster.strength = 3;
        monster.defense = 2;

        while ((player.hp > 0)||(monster.hp > 0))
        {
                if (player.defense < monster.strength)
                        player.hp = player.hp - (monster.strength - player.defense);

                if (monster.defense < player.strength)
                        monster.hp = monster.hp - (player.strength - monster.defense);

                cout << " player.hp =" << setw(3) << player.hp
                         << " player.defense  =" << setw(2) << player.defense
                         << " player.strength =" << setw(2) << player.strength << endl;
                cout << " monster.hp=" << setw(3) << monster.hp
                         << " monster.defense =" << setw(2) << monster.defense
                         << " monster.strength=" << setw(2) << monster.strength << endl << endl;
        }
}
Thanks Gonado! I don't know how I didn't realize this but I used the wrong logic for that but not anything else, I've been trying to fix this for a week and I looked right at the problem a million times, just passing it over!
Topic archived. No new replies allowed.