Sunday, July 12, 2009

C ++ problem?

whats the craic lads,





im writing a program at the moment and have an if statement in it





this is some of the code...





const int ROWS = 30;


const int START_X = 9;


const int START_Y = 4;





int start_x;


int start_y;


bool mouse_at_seat;





for(;;)


{


if(start_x %26lt; START_X + 2 || start_x %26gt; START_X + ROWS * 2 || start_y %26lt; START_Y + 2 ||start_y %26gt; START_Y + 10 || start_y - START_Y % 2!= 0 || start_y == 10 )


{


mouse_at_seat = false;


}


where_mouse(start_x,start_y);


gotoxy(start_x,start_y);


cout %26lt;%26lt; 'B' ;


}





what the program is meant to do is display an image of a plane(which it does!) and then by using the mouse you can click on any seat on the plane and book it, when a seat is booked 'B' will appear onthe seat.





unfortunately with my program you can click all over the plane and the letter 'b' will appear...





thats what the big long IF statement is there to prevent but its not working..





any ideas where have i went wrong?

C ++ problem?
You may contact a c++ helper live at website like


http://ccietutorial.com/
Reply:Hello


May I suggest the use of the ternary facility nested like this:








mouse_at_seat = (start_x %26lt; START_X + 2) ? false :


(start_x %26gt; START_X + ROWS * 2) ? false :


(start_y %26lt; START_Y + 2) ? false :


(start_y %26gt; START_Y + 10)? false :


(start_y - START_Y % 2!= 0) ? false :


(start_y == 10 ) ? false : true;


if (mouse_at_seat)


{


printf("TRUE\n");


}


else


{


printf("FALSE\n");


}











Using this you won't need the infinite loop


Regards


G
Reply:Isn't that an infinite loop? And if the if statement is true, you're still going to be in the loop. I think you need to put a condition in the loop or kill the for loop and move you if statement elsewhere.
Reply:Use parentheses in your if statement.


Not sure if it will fix the problem but it'll work better.





It should look like this:





if(


(start_x %26lt; (START_X + 2))


|| (start_x %26gt; (START_X + ROWS * 2))


||


etc...


No comments:

Post a Comment