all my booleans turn false, but why?

i'm working on a relatively simple game. I'm using an array of booleans which will mean an enemy is alive or dead. If benemies is true, the enemy is still alive, and false means dead. After the enemies are attacked for the first time, all of the booleans are apparently turning false (dying.) The game will not continue because the enemies are "dead." Can anyone see why this is happening?

main(); calls attack(); and everything goes fine until attack(); calls repeatattack();, then benemy[20] turns false for all elements. Here is the file with the problem code:

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 "stdafx.h"
#include  "math.h"
#include <string>
#include <iostream>
#include <time.h>

using namespace std;

double damage;
int a[1250];
double chrhp[20];
string enemy[20];
char letter[20];
bool benemy[20]; //here is benemy whos elements turn false
int enemyxy[20];
int mapcorrecter;
int numberofenemies;
char enemydatabase[5][15];
void databasefill(){
  strcpy_s(enemydatabase[1], "Flower");
  strcpy_s(enemydatabase[2], "Physicist");
  strcpy_s(enemydatabase[3], "Biologist");
  strcpy_s(enemydatabase[4], "Chemist");
  strcpy_s(enemydatabase[5], "Astronomer");
}


double evaldmg(char mag[1], char geo[1], int x, int y, int r){
  int ini;
  char f[] = "f";
  char c[] = "c";
	  
	    if (strcmp (mag,f) != 0){ ini = 100;}
       double dmg;
       double pi = 3.14159265358979323846;
        if (strcmp (geo,c) != 0){dmg = (ini / (pi * r * r));}
		cout<<"Enemy takes "<<dmg<<" damage"<<endl;
		cout<<"ini "<< ini << " r " <<r<< " pi "<< pi << endl;//for programmer

return (dmg);}


void callenemies(){
srand ( time(NULL) );

  databasefill();
  int x = rand() % 9+ 1; //rnd 1-9 # enemies
  int random = rand() % 20+ 1; //rnd 1-20
 
    for (int i=0; i<x;i++){
      int pickenemy = rand() % 5+ 1; //rnd 1-5
      enemy[i] = enemydatabase[pickenemy];
	  letter[i]= enemydatabase[pickenemy][0];
      benemy[i] = true;
  int y = rand() % 1250+1; //rnd 1-1250 for xy position
      enemyxy[i] = y;
	  chrhp[i] = 100;
                           }cout<<"enemiescalled";//for programmer
};


void hitenemy(double d,string enemname, char chr,int chrnumber){

chrhp[chrnumber] = chrhp[chrnumber] - d;
cout<< enemname << " was struck for " <<d<<endl;
if (chrhp[chrnumber] <= 0){cout<< enemname<<" is dead."<<endl; benemy[chrnumber]=false;}
}

void clearenemies(){ /* this function could be the culprit, but after this,
callenemies(); is called which creates enemies and make some benemies==true */
 for (int i=0; i<20;i++){ //clear data
     enemyxy[i] = 0;
	 chrhp[i]   = 0;
	 benemy[i]  = false;
     enemy[i]   = "";
	 
                        }
cout<<"clearenemies";//for programmer
}

void endfight(){
cout<<"fight complete";}
void removedead(){
for (int i=0;i<20;i++){
		if (benemy[i] = false){
		   enemyxy[i] = 0;
	       chrhp[i]   = 0;
	       enemy[i]   = "";
		     cout<<"enemy removed";}//for programmer 
	                  }

}
void repeatattack(){
cout<<"repeatattack";//for programmer
	int chk=0;
	for (int i=0;i<20;i++){ //this checks for any alive enemies
		if (benemy[i] == true){chk=chk+1;}// here is where the problem shows itself
	                      cout<<chk;}//the prog shows 20 zeros!!???
	if (chk > 0){string magic[20]; //this code never gets executed
	magic[0] = "Flash";
string geo[20];
    geo[0]   = "Circle";

	cout << "Choose your magic:"<< endl;
    showmagic(magic);
	char magopt[1];
	cin >> magopt;

	cout << "Choose your Geometry:"<< endl;
    showabilities(geo);
	char abiopt[1];
	cin >> abiopt;
	
	cout << "Enter X Y Radius:"<< endl;
	int x,y,r;
	cin >> x >> y >> r;
	damage = evaldmg(magopt,abiopt,x,y,r);
	  
		for (int i=0; i<20; i++){
			if (benemy[i]==true){hitenemy(damage,enemy[i],letter[i],i);} //must check for hit

		                        }
removedead();


}
    if (chk = 0){endfight();}
cout<<"atk repeated"<<chk;//for programmer
}


void prepfirstattack(){
      clearenemies();
	  callenemies();
      showmap(enemyxy,letter);
}
void attack(){
string magic[20];
	magic[0] = "Flash";
string geo[20];
    geo[0]   = "Circle";

	cout << "Choose your magic:"<< endl;
    showmagic(magic);
	char magopt[1];
	cin >> magopt;

	cout << "Choose your Geometry:"<< endl;
    showabilities(geo);
	char abiopt[1];
	cin >> abiopt;
	
	cout << "Enter X Y Radius:"<< endl;
	int x,y,r;
	cin >> x >> y >> r;
	damage = evaldmg(magopt,abiopt,x,y,r);
	  
		for (int i=0; i<20; i++){
			if (benemy[i]==true){hitenemy(damage,enemy[i],letter[i],i);} //must check for hit

		                        }
removedead();
repeatattack(); 

}


thanks for any help
Don't know if this causes the problem , but this is wrong:

1
2
3
4
5
6
7
8
9
10
11
12
void endfight(){
cout<<"fight complete";}
void removedead(){
for (int i=0;i<20;i++){
		if (benemy[i] = false){ //you mean - if (benemy[i] == false)
		   enemyxy[i] = 0;
	       chrhp[i]   = 0;
	       enemy[i]   = "";
		     cout<<"enemy removed";}//for programmer 
	                  }

}


I skimmed it - what bluezor said looks correct. If you use '=' in an if statement it won't check to see if something equals false in your case. It'll set the left hand side "benemy[i]" to false every time it gets to that line.

You should really try to not put some of your if statements on the same line as the body of the if statement. Try to neaten it out. It'll make it easier for you to read and others.
Thanks so much for the quick answer. It worked! That line was making them all false. It's interesting that "benemy[i]=false" was executed even though it was in the () of an if statement.
Thanks mythios, I'll do that. I've never taken a course or anything for c++ - I guess that's why I don't know to do some little things like that.
What IDE are you using? (The program you are writing your code in)
i'm using vs 2008 express
Select all your code (ctrl + a) - Then go to Edit->Advanced->Format Selection. It'll neaten out some of your code.
that's great, i'll definately be using that with all my projects. ty!
There is a design strategy that you can use to avoid this in the future. You have to understand the root cause.

1
2
3
4
5
6
7
if(value = false) 
{
   // value is assigned the value of false but this block will never 
   // execute because the result of the expression is false
   // = is the assignment operator
   // == is the relational operator that checks for equality
}


Try this. It won't compile because "false" is not an LValue. You can't assign to false and
the compiler knows that.
1
2
3
if(false = value) 
{
}


Alternatively, this is okay as well because there is no possibility of an assignment causing
problems.
1
2
3
if(!value)
{
}


The same strategy can be used for anything else. Accidental misuse of the assignment operator can be avoided by employing this strategy in the future.
1
2
if("end" == theString)
if(25 == theIntegerValue)
It's interesting that "benemy[i]=false" was executed even though it was in the () of an if statement.

That's why you can do this: if ((ch = getchar()) == '\n') user.pressed_enter == true;
No problem :)
Topic archived. No new replies allowed.