! operator problem

Started by
4 comments, last by dwmitch 20 years, 1 month ago
I''ve been messing around with a simple text based battle system on the console (hopefully I can edit it to work when I get D3D working) for an RPG and I''ve been having trouble with the !(not) operator. I have a member function of the player class to process the commands (Defend, Attack Power 1/3, 1/2, 1, and soon to be magic), and when the player chooses to defend it''s supposed to set Dmg (the damage variable) to 0. If I don''t do that it says that I hit the enemy (in this case peasant) for -899389 points, which gives him an absurd amount of HP. Here''s the relavant code:

//player attack function
Player::Attack {
.
.
.
.
switch (Commands) {
case (com_Defend):
player.Defend = true;
cout << "You defend";
Dmg = 0;
break;
.
.
.
//*******************

//Main function
.
.
.
Dmg - player.Attack();
if (Dmg = 0 && !player.Defend) {
cout << "You missed\n";
}
.
.
.
 
The problem is when I defend I get "You defend" "You missed". It seems to be ignoring the not operator, and I''ve tried putting !player.Defend in parentheses. I''ve also tried nested if statements:

if (Dmg = 0) {
if (!player.Defend) { //also tried if !(player.Defend)
cout << "You missed";
}
 
which yielded the same results. By the way, I''m sorry if this has been addressed before, but my search turned up nothing.
300In the land of the proud and freeyou can sell your soul and your dignityfor fifteen minutes on tvhere in Babylon.
Advertisement
It sounds unnecessary, but what happens when you set

if (Dmg = 0) {
if (!player.Defend) { //also tried if !(player.Defend)
cout << "You missed";}

TO

if (Dmg = 0) {
if (player.Defend != true) { //also tried if !(player.Defend)cout << "You missed";}

I''m just trying to make sure that it''s this snippet of code that''s causing the problem.
well one problem is that you are saying:

dmg = 0

when you should be saying

dmg == 0

i don't think that's your problem but it's definitely going to be a problem later

-me

[edited by - Palidine on March 18, 2004 8:14:26 PM]
Shouldn''t all your single equal sign operators be double equal sign operators? "=" actually assigns values to variables, while "==" just compares them for equality.

Or maybe you just wrote stuff out too quickly and made a typo...

int Agony() { return *((int*)0); } Mwahaha... >8)
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
also

dmg - player.Attack();

doesn't do anything

you want either:

dmg = dmg - player.Attack();

or

dmg -= player.Attack();

[EDIT: my guess is that since you are making simple mistakes like the 2 i've pointed out, the problem may lie somewhere other than in the code that you have posted]

-me

[edited by - Palidine on March 18, 2004 8:17:31 PM]
Thanks, guys. I always forget to use == for if statements. I took up BASIC on a salvaged Commodore 64 when I was about 14, forgot about it, then got into C++ four years ago and I''m still having trouble making the switch. I just now got over the fact that there''s no then statement.

The Dmg - player.Attack() was a typo in the post. I was in a hurry and I hit "-" instead of "=". I''m about six miles away from my compiler, so I can''t try it right now, and I''ve found a workaround (all of the couts in the switch statement), but this will be useful in the future.
300In the land of the proud and freeyou can sell your soul and your dignityfor fifteen minutes on tvhere in Babylon.

This topic is closed to new replies.

Advertisement