Public Group

Archived

This topic is now archived and is closed to further replies.

! operator problem

This topic is 5302 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

Share on other sites
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.

Share on other sites
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]

Share on other sites
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)

Share on other sites
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]

Share on other sites
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.

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
14

• 14
• 10
• 23
• 9
• 32
• Forum Statistics

• Total Topics
632631
• Total Posts
3007532
• Who's Online (See full list)

There are no registered users currently online

×