If..Else?

Started by
4 comments, last by Wachar 21 years, 11 months ago
Why does this if..else statement always execute the first block of code only every single time?
  
	if(playerRace = HUMANS)
	{
		human1.humanSetData();
		human1.main_human_story();
	}
	else
	{
		alien1.alienSetData();
		alien1.main_alien_story();
	}
  
(HUMANS is #define''d for the value of 1 and there''s an ALIENS #define for 2) What does it take to be a good game programmer? --- Good insight, good knowledge, and of course, big money deposits!
Wachar's Eternity <-<-<-<-<- Me own site!
Advertisement
it needs to be:

==

NOT

=


a single = is an assignment operator the expression x = y always evaluates to false

== is the are these things equal check

-me
It should be:

if(playerRace == HUMANS)

Note the double equals sign. If you only use one (as in variable = 1) it sets the variable to equal 1. If you use two (variable == 1) then it compares the two (which is what you want).

-----------------
"Who the hell wants to hear actors talk?" - H. M. Warner

"Everything that can be invented has been invented." - Charles H. Duell

"I think there is a world market for maybe five computers." - Thomas Watson

[edited by - StarFire on May 3, 2002 7:11:36 PM]
-----------------"Who the hell wants to hear actors talk?" - H. M. Warner"Everything that can be invented has been invented." - Charles H. Duell"I think there is a world market for maybe five computers." - Thomas Watson, Chairman of IBM (1943)
If HUMANS != 0, and you set playerRace = HUMANS, then it will always be true! You want to use the ''==''.

Billy - BillyB@mrsnj.com
(Hey, I''''m no longer anonymous!)
Doh! I gotta remember that! I ALWAYS *repeat* - ALWAYS forget to do == ''equals to'' rather than = ''is now...". Thanks guys!


What does it take to be a good game programmer?
---
Good insight,
good knowledge,
and of course,
big money deposits!


Wachar's Eternity <-<-<-<-<- Me own site!
quote:Original post by Wachar
I ALWAYS *repeat* - ALWAYS forget to do == 'equals to' rather than = 'is now...".


If you have this problem, you can try to get into the habit of placing any constant values on the left-hand side of the comparison expression:

if(HUMANS == playerRace) {    human1.humanSetData();    human1.main_human_story();} 


That way, your compiler will complain if you accidentally use '=' instead of '=='.

[edited by - spock on May 4, 2002 5:44:55 AM]

This topic is closed to new replies.

Advertisement