D&D Combat Program Errors

So I'm trying to design this program for a class project however I've hit a snag in regards to getting it to run. Specifically in the Dragon function on line 99, the issue starts at line 105 giving me an error about an expected primary-expression before '.' token. Any help would be greatly 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
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
  #include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void Dragon();
void Goblin();

struct Entity
{
	int hp;
	int ac;
	int strmod;
	std::string name;

	Entity()
		: hp(1), ac(1), strmod(1), name("unknown-entity")
	{

	}
	
	inline void setName(std::string newName) { name = newName; }
	int getIntegerInput();
};

int Entity::getIntegerInput()
{
	int inputValue = 1;

	do
	{
		std::cin >> inputValue;
	} while (inputValue <= 0);

	return inputValue;
}

struct Enemy : public Entity
{

};

struct Player : public Entity
{
	int profmod;

	Player()
		: profmod(1)
	{

	}
	void create();
};

void Player::create()
{
	//Character HP Entry and Validation
	std::cout << "Set " << name.c_str() << "'s HP: ";
	hp = getIntegerInput();

	//Character AC Entry and Validation
	std::cout << "Set " << name.c_str() << "'s Armor Class: ";
	ac = getIntegerInput();

	//Character Proficiency Modifier
	std::cout << "Set " << name.c_str() << "'s Proficiency Modifier: ";
	profmod = getIntegerInput();
}

int main()
{
	Enemy dragon;
	Enemy goblin;
	Player soldier;
	int selection;
	
	std::cout << "Welcome to the D&D simple combat processor!" << std::endl;
	std::cout << "To begin, please enter the following info about your character." << std::endl;
	soldier.setName("Character");
	soldier.create();
	std::cout << "Now select your enemy! Type the number pertaining to the enemy you wish to face:\n";
	std::cout << "---------------------------------------------------------\n";
	std::cout << "1. Goblin\n";
	std::cout << "2. Dragon\n";
	
	std::cin >> selection;
	
	while(selection < 1 || selection > 2)
		{
			std::cout << "That is an incorrect selection, try again.\n";
		}
		
	
	system("pause");
	return 0;
}

void Dragon()
{
	int d_hp = 160;
	int d_ac = 18;
	int d_strmod = 5;
	int roll;
	while(d_hp > 0 || Entity.hp > 0 )
		{
			//Player turn
			roll = 1 + rand() % 20;
			if(roll + Player.strmod + Player.profmod > d_ac)
				{
					d_hp-=Player.strmod;
				}
			//Dragon turn
			roll = 1 + rand() % 20;
			if(roll + d_strmod > Player.ac)
				{
					Player.hp-= d_strmod;
				}
		}
	if(Player.hp <= 0)
	{
		std::cout << "Player is dead.\n"
		return;
	}
	if(d_hp <= 0)
	{
		std::cout << "Dragon is dead.\n";
		return;
	}
}
Both Player and Entity are Types, not instances.

What are you really trying to do in that function?

Perhaps you meant to pass some variables into that function as parameters?

The function is meant to essentially run the combat simulation. So it needs to work as follows:
1. "Roll a 20 sided dice."
2. Compare the rolled # + the player's strength mod and proficiency mod to the Dragon's AC value.
3. If the result is higher, then subtract the player strength mod value from the Dragon's HP value.
4. If it's lower, it misses and nothing happens.
5. It then rolls a new number for the dragon's turn.
6. Executes the same steps as above but reversed so it's the dragon attacking the player.
7. This should continue to repeat until one of the 2 combatants is dead, their HP reaching 0 or lower.
Entity is a type you created.

Just like int is a type built into the language.

You didn't say int = 18;
You created an instance of the type:
int d_ac = 18;or
1
2
int d_ac;
d_ac = 18;


In the same way you need something like

1
2
3
Entity xyz;
xyz.setname("maxwellmachoman");
int abc = xyz.getIntegerInput();


and so on.

Please note that the design of Player and Entity is problematic in other ways.
Lines 89-92: You have an infinite loop. If selection is out of range, you will repeatedly output the message and never exit the loop.

Line 105: As others pointed out, Entity is a type. You can't store something in a type, you have to store something in an instance of a type.

Line 106: This loop will continue until both players are dead. You want to use && so that play stops when one player dies.

Line 109,111,117,120: Player is a type, not an instance.

Line 122: Missing ;


Topic archived. No new replies allowed.