problem with if statement
-
Hi all im being a bit thick at the moment and my if statement does not seem to be working as it should. i have a CString variable called str_Status this holds the valued "Cancelled by User" when i step through my program and come to the below if statement if ((str_Status != "Sent") || (str_Status != "Cancelled by User")) i expect it not to go into the if, but it does :(( and i dont know why can any see whats wrong here, it seems to be with the "or", but i just cant get it to work thanks in advance si
-
Hi all im being a bit thick at the moment and my if statement does not seem to be working as it should. i have a CString variable called str_Status this holds the valued "Cancelled by User" when i step through my program and come to the below if statement if ((str_Status != "Sent") || (str_Status != "Cancelled by User")) i expect it not to go into the if, but it does :(( and i dont know why can any see whats wrong here, it seems to be with the "or", but i just cant get it to work thanks in advance si
Hi, As far as I can see in your example any string will go into the if code. ie "Cancelled by User" != "Sent" .... therefore it goes into if (the first test) "Sent" != "Cancelled by User" .... therefore it goes into if (the second test) "Anything else" != "Sent" ........ therefore it goes into if (the first test) I think you may need to brush up on your or and ands, then try again! Happy coding, Ali
-
Hi, As far as I can see in your example any string will go into the if code. ie "Cancelled by User" != "Sent" .... therefore it goes into if (the first test) "Sent" != "Cancelled by User" .... therefore it goes into if (the second test) "Anything else" != "Sent" ........ therefore it goes into if (the first test) I think you may need to brush up on your or and ands, then try again! Happy coding, Ali
-
Hi all im being a bit thick at the moment and my if statement does not seem to be working as it should. i have a CString variable called str_Status this holds the valued "Cancelled by User" when i step through my program and come to the below if statement if ((str_Status != "Sent") || (str_Status != "Cancelled by User")) i expect it not to go into the if, but it does :(( and i dont know why can any see whats wrong here, it seems to be with the "or", but i just cant get it to work thanks in advance si
-
It might be better to treat it as a truth table - Alison is right. treat a as your first string check, eg:
bool a = (str != "Whatever");
treat b as your second one:bool b = (str != "Something else");
then you have (effectively):if(a || b) { // do stuff }
So if str is "Whatever", then a == false, but b == true, so (a || b) == true as well, and so it goes into the if If str is "Something else", then a == true, b == false, so (a || b) == true again, so it goes into the if If str is any other string, then a == true, b == true, so (a || b) == true, and so it goes into the if. What you probably want is:if( (str != "Whatever) **&&** (str != "Something else") ) { // do stuff. }
-- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky -
Hi all im being a bit thick at the moment and my if statement does not seem to be working as it should. i have a CString variable called str_Status this holds the valued "Cancelled by User" when i step through my program and come to the below if statement if ((str_Status != "Sent") || (str_Status != "Cancelled by User")) i expect it not to go into the if, but it does :(( and i dont know why can any see whats wrong here, it seems to be with the "or", but i just cant get it to work thanks in advance si
If I understand it correctly, you don't want it to run the block of code if status = "sent" or "cancelled by user". Try doing the following if statement: if (!((str_Status == "Sent") || (str_Status == "Cancelled by User"))) If it's equal to "Sent", then it returns false If it's equal to "Cancelled by User", then it returns false If it's equal to "Anything else", then it returns true. Hope this helps, Tim
-
Hi all im being a bit thick at the moment and my if statement does not seem to be working as it should. i have a CString variable called str_Status this holds the valued "Cancelled by User" when i step through my program and come to the below if statement if ((str_Status != "Sent") || (str_Status != "Cancelled by User")) i expect it not to go into the if, but it does :(( and i dont know why can any see whats wrong here, it seems to be with the "or", but i just cant get it to work thanks in advance si
Step through the
!=
operator code (F11 in the debugger) to see the logic flow. First it testsstr_status != "Sent"
. Sincestr_status
is, in fact, not equal to"Sent"
, the left side of the||
is true. Since||
short-circuits in C, the||
evaluation immediately stops and returnstrue
. Since theif
test istrue
, the block of code following theif
runs. What you want isif ( str_Status != "Sent" && str_Status != "Cancelled by User")
&& not || --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | RightClick-Encrypt | 1ClickPicGrabber Kosh reminded me of some of the prima-donna programmers I've worked with. Knew everything but when you asked them a question; never gave you a straight answer. -- Michael P. Butler in the Lounge
-
Hi all im being a bit thick at the moment and my if statement does not seem to be working as it should. i have a CString variable called str_Status this holds the valued "Cancelled by User" when i step through my program and come to the below if statement if ((str_Status != "Sent") || (str_Status != "Cancelled by User")) i expect it not to go into the if, but it does :(( and i dont know why can any see whats wrong here, it seems to be with the "or", but i just cant get it to work thanks in advance si
In an or test || if the first condition is true the second conditon is not even tested for, thus in your if ((str_Status != "Sent") || (str_Status != "Cancelled by User")) (str_Status != "Sent") is true so the if statement is executed. Just use (str_Status != "Cancelled by User") as the sole condition.