Need help with next step

May 23, 2013 at 2:21am
Ok so, the code I have below compiles, however I am not sure what I need to do to accomplish my next step.

1st question : As you can see I have tons of ints. I have seen people do something like hero->hp, or victim->damage_roll. I am wondering how I would accomplish this as I think it would be easier to understand when reading the code.

2nd question : I would like to break apart the getting of the names from the actual combat code. When I do a void of getnames and include it in the program it works as intended *until* when the program terminates to declare a winner, then it just gives me a *blank space* loses *blank space* wins.

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
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std; 


class KeepRunning
{
  public:
    ~KeepRunning() {
      // Whatever code you prefer to keep the program running, e.g.
      system("pause"); // "Press enter"
				    }
};



void combat()
{
	srand ((unsigned) time(NULL) );
	
	int Uhp = 50, Vhp = 50, udamage_roll, min_dam = 1, max_dam = 10;
	int ublock, uarmor, vblock,varmor, vdamage_roll, min_block = 1, max_block = 6;
	int Ulifesteal;
	int Vlifesteal;

	string name;
	string victim;
	
	cout <<"Your name: ";
	cin >> name;

	cout <<"Victim name: ";
	cin >> victim;
	cout << endl;


	while (((Uhp > 0) && (Vhp > 0)) && ((Uhp < 100) && (Vhp < 100)))
	{

	ublock = rand()% (max_block-min_block) + min_block;
	vblock = rand()% (max_block-min_block) + min_block;

	vdamage_roll = rand() % (max_dam-min_dam) + min_dam;
	udamage_roll = rand() % (max_dam-min_dam) + min_dam;

	Vlifesteal = rand()%2;
	Ulifesteal = rand()%2;

	cout <<"You do " << udamage_roll << " damage" << endl;
	cout << victim << " blocks " << vblock << " damage " << endl <<endl;

		if ((udamage_roll - vblock) < 0)
	{
		udamage_roll = 0;
		vblock = 0;
	}

		if ((Ulifesteal == 1) && (udamage_roll > vblock))
			Ulifesteal = udamage_roll - vblock;	
		else		
			Ulifesteal = 0;
		

	
	cout << victim << " does " << vdamage_roll << " damage" << endl;
	cout <<"You block " << ublock << " damage" <<endl << endl;
	
		if ((vdamage_roll - ublock) < 0)
		{
			vdamage_roll = 0;
			ublock = 0;
		}

		if ((Vlifesteal == 1) && (vdamage_roll > ublock))	
			Vlifesteal = vdamage_roll - ublock;	
		else		
			Vlifesteal = 0;
		

	Vhp = Vhp - (udamage_roll - vblock) + Vlifesteal;
	Uhp = Uhp - (vdamage_roll - ublock) + Ulifesteal;

	cout << "You have: " << Uhp << " Lifesteal : " << Ulifesteal <<" "<< endl;
	cout << "Victim has : " << Vhp << " Lifeseal : " << Vlifesteal <<" "<< endl << endl; 

	if (( Uhp <= 0) && ( Vhp <= 0))		
		cout << "Its a tie, neither player wins" << endl;		
	else if (Uhp <= 0)		
		cout << name << " loses " << victim << " wins " <<endl;		
	else if(Vhp <= 0)		
		cout << victim << " loses " << name << " wins " <<endl;
		
	
	if (( Uhp > 100) && ( Vhp > 100))		
		cout << "Its a tie, neither player wins" << endl;		
	else if (Vhp > 500) 		
		cout << name << " loses " << victim << " wins " <<endl;		
	else if (Uhp > 500)		
		cout << victim << " loses " << name << " wins " <<endl;
		
	}
}

int main()
{
KeepRunning kr;
	
	combat();

return 0;
}
May 23, 2013 at 2:33am
Put the related variables in a class, so they are actually member variables and not just some random values in a single function.

operator-> is a conjunction of (*). as in:
1
2
3
4
MyClass* myobj;
   (*myobj).mymember;
//Equivalent to
   myobj->mymember;


I suggest going over this site's tutorial on classes, reformatting your code, and posting your updated code for us to look at.

I laud you for going through the second thread and actually reading up on how to keep the console open. However, try to do better than system("pause") in the destructor.
May 23, 2013 at 4:01am
So would it be something like this, and then use hero->hp. I read over the tutorial on classes (Class I), and still am slightly confused. Sorry if I dont seem to get it, I am the type of person that has a hard time grasping the concept, but once I figure it out or shown an example regarding what im doing then it clicks and sticks with constant repetition. Also would I be able to essentially double stack variables. such as...

Hero has those 4 attributes below, then I want to do the same thing with Victim, would I be able to keep those same var names and not have it screw up numbers?


1
2
3
4
5
6
7
class hero;
{
	int hp;
	int damage_roll;
	int block;
	int lifesteal;
};
May 23, 2013 at 4:03am
If you have it like that, it won't work. All of those are private. If you are going to store various things of which all are public, I recommend replacing "class" with "struct" (structure). That way, they're implicitly public (though really, you could just stick a public: in front of those variables and get the same result, but I prefer using structures for cases like these).
May 23, 2013 at 4:09am
Yes, the names can be kept as is. Instead of restricting the class to one "object," you can make it general as:
1
2
3
4
5
6
7
8
9
10
11
struct player{ //struct so the members are public; i.e. can be accessed outside of the class
   int hp;
   int damage_roll;
   int block;
   int lifesteal;
};
//...
int main(){
   player hero, victim; //Two distinct objects;
      hero.hp = victim.hp = 50; //variables belongs to different objects; object identifies which one to use
}


Don't forget to read up on public vs. private and member functions (especially constructors and destructors).
Last edited on May 23, 2013 at 4:09am
May 26, 2013 at 12:29am
Ok so i set up the struct, and ran into compile issues.

Here is the error im running into :
(19) : error C2447: '{' : missing function header (old-style formal list?)

(31) : error C2079: 'hero' uses undefined struct 'player'

(31) : error C2079: 'victim' uses undefined struct 'player'

(32) : error C2227: left of '->hp' must point to class/struct/union/generic type
1> type is 'int'

(32) : error C2227: left of '->hp' must point to class/struct/union/generic type
1> type is 'int'

(49) : error C2227: left of '->hp' must point to class/struct/union/generic type
1> type is 'int'

the rest of the errors are pretty much the same as these. I tried doing the code with both -> and replacing -> with a .

Also in the main int() I initiate the combat(), would i need to include anything in the parameter field?
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
struct player;
{
		int hp;
		int	damage_roll;
		int block;
		int lifesteal;
};



void combat()
{
	srand ((unsigned) time(NULL) );
	player hero, victim;
		 hero->hp = victim->hp = 50;

	int min_dam = 1, max_dam = 10;
	int min_block = 1, max_block = 6;


	string name;
	string villan;
	
	cout <<"Your name: ";
	cin >> name;

	cout <<"Victim name: ";
	cin >> villan;
	cout << endl;


	while (((hero->hp > 0) && (victim->hp > 0)) && ((hero->hp < 100) && (victim->hp < 100)))
	{
...
May 26, 2013 at 12:35am
You have a misplaced semi-colon on the very first list. the compiler treats line 1 as a prototype and rest to be a function block missing a function name and parameters.
Lines 14 and 15
class->member as I have said before is equivalent to (*class).member.
Your hero and victim objects are not pointers, so you must use class.member.
Last edited on May 26, 2013 at 12:39am
May 26, 2013 at 2:05am
Here is the code, compiled and works just like before, but with the new additives. Thank you everyone. Now, excluding my KeepRunning system pause (which i know is something to stay away from, but it serves the purpose I need as its only me dealing with the code), what would you critique about the format of the code, is it easy to read (I know i dont have internal documentation, but the code itself). What could be done different that would make this more efficient. Ill continue to work with the struct and class and read further into them and continue to add onto this program with what I learn. Practice makes perfect :)

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 <cstdlib>
#include <string>
#include <ctime>
using namespace std; 


class KeepRunning
{
  public:
    ~KeepRunning() {
      // Whatever code you prefer to keep the program running, e.g.
      system("pause"); // "Press enter"
				    }
};


struct player
{
	int hp;
	int damage_roll;
	int block;
	int lifesteal;
};

void combat()
{
	int min_dam = 1, max_dam = 10;
	int min_block = 1, max_block = 6;
	srand ((unsigned) time(NULL) );

	
	player hero, victim;
		 hero.hp = 50;
		 victim.hp = 50;

	string name;
	string villan;
	
	cout <<"Your name: ";
	cin >> name;

	cout <<"Victim name: ";
	cin >> villan;
	cout << endl;

	
	while (((hero.hp > 0) && (victim.hp > 0)) && ((hero.hp < 100) && (victim.hp < 100)))
	{
		 hero.block = rand()% (max_block-min_block) + min_block;
		 victim.block = rand()% (max_block-min_block) + min_block;

		 hero.damage_roll = rand() % (max_dam-min_dam) + min_dam;
		 victim.damage_roll = rand() % (max_dam-min_dam) + min_dam;

		 hero.lifesteal = rand()%2;
		 victim.lifesteal = rand()%2;


	cout <<"You do " << hero.damage_roll << " damage" << endl;
	cout << villan << " blocks " << victim.block << " damage " << endl <<endl;

		if ((hero.damage_roll - victim.block) < 0)
	{
		hero.damage_roll = 0;
		victim.block = 0;
	}

		if ((hero.lifesteal == 1) && (hero.damage_roll > victim.block))
			hero.lifesteal = hero.damage_roll - victim.block;	
		else		
			hero.lifesteal = 0;
		

	
	cout << villan << " does " << victim.damage_roll << " damage" << endl;
	cout <<"You block " << hero.block << " damage" <<endl << endl;
	
		if ((victim.damage_roll - hero.block) < 0)
		{
			victim.damage_roll = 0;
			hero.block = 0;
		}

		if ((victim.lifesteal == 1) && (victim.damage_roll > hero.block))	
			victim.lifesteal = victim.damage_roll - hero.block;	
		else		
			victim.lifesteal = 0;
		

	victim.hp = victim.hp - (hero.damage_roll - victim.block) + victim.lifesteal;
	hero.hp = hero.hp - (victim.damage_roll - hero.block) + hero.lifesteal;

	cout << "You have: " << hero.hp << " Lifesteal : " << hero.lifesteal <<" "<< endl;
	cout << "Victim has : " << victim.hp << " Lifeseal : " << victim.lifesteal <<" "<< endl << endl; 

		if ((( hero.hp <= 0) && ( victim.hp <= 0)) || (( hero.hp > 100) && ( victim.hp > 100)))	
			cout << "Its a tie, neither player wins" << endl;		
		else if ((hero.hp <= 0)	|| 	(victim.hp > 500))
			cout << name << " loses " << villan << " wins " <<endl;		
		else if ((victim.hp <= 0)	|| (hero.hp > 500))
			cout << villan << " loses " << name << " wins " <<endl;
		
	}
}



int main()
{
KeepRunning kr;
	
	combat();

return 0;
}

Last edited on May 26, 2013 at 2:19am
Topic archived. No new replies allowed.