help needed plus triple plz

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

help needed plus triple plz

Post by omar shaaban »

well here are my qustions(with my answers :P )
1- sould stament "else" after if if it is not true like this

Code: Select all

if(a==b)
{
action1
}
else
if(a==c)
{
action2
}
else
if(a==d)
{
action3
}
suppose that a is not equal any one of those!! so it shouldnt try to do any action!
but what happens that if they are all false it ignore the last "if" condition and do action 3
i only solve it by doing something like this:

Code: Select all

if(a==b)
{
action1
}
else
if(a==c)
{
action2
}
if(a==d)
{
action3
}
else
{
a=a;
}
any help!?
Last edited by omar shaaban on Mon Aug 20, 2007 6:03 pm, edited 1 time in total.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

What is your problem with this ???
(did you forget the else after action2 ???)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post by omar shaaban »

i forgot else while writing post anyway why does he do action3!!?
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

probably because if(a==d) is true ???

with the code you posted is nothing wrong and should work as expected...
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post by omar shaaban »

but a is not equal d!!
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

omar shaaban wrote:but a is not equal d!!
if so, action3 will not be executed... ;)

I think there is something wrong with the rest of your code (that parts you didn't post)...
Or your compiler does strange errors, but I doubt this... :P
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Maybe you forgot a ; behind the if (a==d);?
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post by omar shaaban »

will it worked after i put this after the code :shock: :?

Code: Select all

else 
{ 
a=a; 
}
strange!!?
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

very strange, this can not be, there must something else be wrong !!! :shock:
Maybe post the complete (real) code part of this...
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
AlexL
Posts: 184
Joined: Tue Mar 02, 2004 6:06 pm
Location: Washington State

Post by AlexL »

Or maybe to help clean up the jumble of ifs, ands, or buts you could try doing something like the code below. As you can see it's much cleaner and may help you out with forgetting a else or a bracket somewhere, and depending on what compiler your using and how you have it set to optimize, it may also be faster then an if/else nest ;) But that's a whole different discussion that we don't need to get in to.

Code: Select all

int a = 1;

switch(a)
{
case 1:
	action1();
	break;

case 2:
	action2();
	break;

case 3:
	action3();
	break;

default:
	printf("Something went wrong, 'a is defined as %d",a);
	a = 0;
	break;
}
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post by omar shaaban »

well here is the real code:

Code: Select all

if(posx>posx2+1||posy<posx2-1)
{
if((mymap[posy2][posx2-rx])==1)
{
    a.destx=posx2-rx;
     a.desty=posy2;
    }
else
if((mymap[posy2][posx2+rx])==1)
{
    a.destx=posx2+rx;
    a.desty=posy2;
    }
    else
if((mymap[posy2-rx][posx2])==1)
{
    a.desty=posy2-rx;
     a.destx=posx2;
    // mymap[posy2-rx][posx2]==4;
    }
else
if((mymap[posy2+rx][posx2])==1)
{
    a.desty=posy2+rx;
     a.destx=posx2;
     //mymap[posy2+rx][posx2]==4;
    }
    else
    {
      [b]if i put these 2 codes it works but if i deleted them it goes wrong![/b]
       //a.desty=a.chary;
     //a.destx=a.charx;
        }
zeno60
Posts: 342
Joined: Sun May 21, 2006 2:48 am
Location: NC, USA
Contact:

Post by zeno60 »

If you take advantage of that wonderful tab button, you will see that you are missing a bracket unless you did not post one at the bottom.

Code: Select all

if(posx>posx2+1||posy<posx2-1)
{
	if((mymap[posy2][posx2-rx])==1)
	{
		a.destx=posx2-rx;
		 a.desty=posy2;
	}
	else if((mymap[posy2][posx2+rx])==1)
	{
		a.destx=posx2+rx;
		a.desty=posy2;
	}
	else if((mymap[posy2-rx][posx2])==1)
	{
		a.desty=posy2-rx;
		 a.destx=posx2;
		// mymap[posy2-rx][posx2]==4;
	}
	else if((mymap[posy2+rx][posx2])==1)
	{
		a.desty=posy2+rx;
		 a.destx=posx2;
		 //mymap[posy2+rx][posx2]==4;
	}
	else
	{
	  [b]if i put these 2 codes it works but if i deleted them it goes wrong![/b]
	   //a.desty=a.chary;
	 //a.destx=a.charx;
	}
Either on the last line, or 5th to last line.
Post Reply