Constructors and consts

I can't tell exactly what is going on with this code. Its supposed to make the warrior structures lower each others strength (which is held inside the weapon class (a requirement of the course I am in)) by the same amount. It seems like the values keep resetting back to their original after each battle, and I suspect that something is wrong with the way I am doing my constructors, but I imagine there is something more complex at play that I can't figure out.

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

#include <iostream>
#include <vector>
#include <string>
#include <fstream>

using namespace std;

class Weapon {
public: 
	Weapon(const string& name,const int& strength)
		: name(name), strength(strength) {}

	void weapDisplay(){      // part of status
		cout<< " Weapon: " << name << " Weapon Strength: " << strength << endl;
	}
	int getStr() {                   // get strength for battle
		return strength;
	}
	void setStr(const int& newStr){ // set strength after battle
	strength = newStr;
	cout << strength ; 
	}
private:
	string name;
	int strength;
};

class Warrior{
public: 
	Warrior(const string& name, const string& weaponName, const int& weaponStr) //warrior constructor
		:name(name), wpn(weaponName, weaponStr){}
	
void warStat(){
	cout << "Warrior: " << name;
	wpn.weapDisplay();
	}

int warGetStr(){
	int Str= wpn.getStr();
	return Str;
}
void battle(Warrior& enemy){
	cout << name << " battles " << enemy.name << endl;   // displays the combattants
	int w1Str =wpn.getStr();
	int w2Str =enemy.warGetStr();
	if (w1Str == 0){            // this section determines if either or both warriors have 0 str. 
		if (w2Str == 0) {
			cout << "The dead cannot fight the dead."<< endl;
		}
		else{
			cout << enemy.name << " brutally desecrates the already dismembered body." <<endl;
		}
	}
	else if (w2Str == 0){
		cout << name << " brutally desecrates the already dismembered body." <<endl;
	}
	// actual combat:
	
	else {
		int newstr;
		if (w1Str > w2Str){
			enemy.wpn.setStr(0);
			newstr=w1Str- w2Str;
			wpn.setStr(newstr);
			
			cout << name << " slays " << enemy.name << endl;
			}
		else if(w1Str < w2Str){
			newstr= w2Str - w1Str;
			enemy.wpn.setStr(newstr);
			wpn.setStr(0);
			cout << enemy.name << " slays " << name << endl;
		}
		else {
			enemy.wpn.setStr(0);
			wpn.setStr(0);
			cout << "Double Kill. Noone walks out of this battle alive." <<endl; 
		}
	}
	//warStat();
	//enemy.warStat();
}

bool check(const string& checkname){
	bool yes;
	if (checkname == name){
		yes= true;
		}
	else {
		yes= false;
		}
	return yes;
}


private:
	string name; 
	Weapon wpn;


	
};



int main () {
ifstream position;                // opening and checking the file
	position.open ("warriors.txt");
	if (!position){
		cerr << "The games have been canceled, no warriors can be found to fight today";
		exit (17);
	
	}

vector <Warrior> listOfWarriors;       //creating the most important vector of the program

string keyword;
while (position >> keyword){          // this section of code decides which of the commands should be run using an if. 
	if (keyword == "Warrior"){
		string name;
		string weaponName;
		int weaponStr;
		position >> name >> weaponName >> weaponStr;
		Warrior awarrior(name, weaponName, weaponStr);// this line calls the warrior constructor
		listOfWarriors.push_back(awarrior);   
		}
	else if (keyword == "Status"){
		for (size_t i = 0; i < listOfWarriors.size(); i++){
			listOfWarriors[i].warStat();
		}
	}
	else if (keyword =="Battle") {
		string challengerName;            
		string enemyName; 
		Warrior challenger("","",0);
		Warrior enemy("","",0);
		position >> challengerName >> enemyName;        // finds the names of the warriors
		for (size_t i = 0; i < listOfWarriors.size(); ++i) {
			if (listOfWarriors[i].check(challengerName)==true ){ // this checks if the name is the same, to find the right warrior.
				 challenger = listOfWarriors[i];
				//challenger.warStat();
			}
			else if (listOfWarriors[i].check(enemyName) == true){
				 enemy = listOfWarriors[i];
			}

		}
		challenger.battle(enemy);
	}
}
}
What do you mean, give example inputs.

1
2
3
if (w1Str > w2Str){
			enemy.wpn.setStr(0);  //You set the enemy wpn str to 0...
			newstr=w1Str- w2Str; //You are using this w1/w2 stuff, but they are never updated..?? 


Using variables like these..
1
2
int w1Str =wpn.getStr();
int w2Str =enemy.warGetStr();


Makes it harder to follow especially with all the if else. Use something more meaningful.
Using variables like these..
1
2
int warriorStr=wpn.getStr();
int enemyStr=enemy.warGetStr();
Last edited on
Topic archived. No new replies allowed.