Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem with if statement

problem with if statement

Scheduled Pinned Locked Moved C / C++ / MFC
help
8 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    si_69
    wrote on last edited by
    #1

    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

    L B T M S 5 Replies Last reply
    0
    • S si_69

      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

      L Offline
      L Offline
      LittleYellowBird
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • L LittleYellowBird

        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

        S Offline
        S Offline
        si_69
        wrote on last edited by
        #3

        thanks for that, but that makes no sense at all.

        I 1 Reply Last reply
        0
        • S si_69

          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

          B Offline
          B Offline
          Big Art
          wrote on last edited by
          #4

          The statement: if((str_Status != "Sent" || (...)) is always true because you said you set str_Status to "Cancelled by User". Art

          1 Reply Last reply
          0
          • S si_69

            thanks for that, but that makes no sense at all.

            I Offline
            I Offline
            Ian Darling
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • S si_69

              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

              T Offline
              T Offline
              TFrancis
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • S si_69

                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

                M Offline
                M Offline
                Michael Dunn
                wrote on last edited by
                #7

                Step through the != operator code (F11 in the debugger) to see the logic flow. First it tests str_status != "Sent". Since str_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 returns true. Since the if test is true, the block of code following the if runs. What you want is

                if ( 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

                1 Reply Last reply
                0
                • S si_69

                  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

                  S Offline
                  S Offline
                  Stye
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups