Page 1 of 1

help needed plus triple plz

Posted: Mon Aug 20, 2007 2:56 pm
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!?

Posted: Mon Aug 20, 2007 3:14 pm
by Acki
What is your problem with this ???
(did you forget the else after action2 ???)

Posted: Mon Aug 20, 2007 6:04 pm
by omar shaaban
i forgot else while writing post anyway why does he do action3!!?

Posted: Mon Aug 20, 2007 6:18 pm
by Acki
probably because if(a==d) is true ???

with the code you posted is nothing wrong and should work as expected...

Posted: Mon Aug 20, 2007 6:45 pm
by omar shaaban
but a is not equal d!!

Posted: Mon Aug 20, 2007 8:11 pm
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

Posted: Mon Aug 20, 2007 8:38 pm
by hybrid
Maybe you forgot a ; behind the if (a==d);?

Posted: Mon Aug 20, 2007 9:54 pm
by omar shaaban
will it worked after i put this after the code :shock: :?

Code: Select all

else 
{ 
a=a; 
}
strange!!?

Posted: Mon Aug 20, 2007 10:41 pm
by Acki
very strange, this can not be, there must something else be wrong !!! :shock:
Maybe post the complete (real) code part of this...

Posted: Tue Aug 21, 2007 12:30 am
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;
}

Posted: Tue Aug 21, 2007 2:42 am
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;
        }

Posted: Tue Aug 21, 2007 5:49 pm
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.