'while' loop with a logical OR.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Soneji
    New Member
    • Apr 2007
    • 36

    'while' loop with a logical OR.

    Hello yet again!

    I seem to be having a problem with a logical OR inside a while loop.

    Basically, I've got it taking user input for a string, and I want them to type 'exit' to... well, exit.

    Here's a shorter non-working version of my code:

    [Code=cpp]
    #include <iostream>
    #include <string>

    int main()
    {
    std::string str( " " );

    std::cout << "Enter some text. \"exit\" to quit. ";

    while( str != "exit" || str != "Exit" )
    {
    std::cin >> str;
    std::cout << "You entered: " << str << "\n";
    }

    return 0;
    }
    [/Code]

    Ok, I understand why it doesn't work if I type in: Exit. ( because of the short-circuit effect. )

    But, shouldn't it stop the loop if you type in: exit?

    This has been driving me crazy for hours, and everytime I search the site for similar problems,
    It searches for one, or two of the words I enter, and tells me the others give to broad a search. Heh.

    Anyway, I appreciate the help, and if you can point me in the direction of a better
    way to implement this loop, I would appreciate that also.

    Is there a better way to take a single exiting word without having to check for
    every possible letter combination?
    ( exit, Exit, eXit, ExIt, etc. )

    I guess I could put a 'continue' in there, or maybe a break. What do ya think?

    Ok, I'm outs... Lates! ( Oh, and thanks again! )

    -Soneji
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    As you get the string copy it to a temp string and convert it to upper case string and check for EXIT and if it matches then come out of loop.


    Raghuram

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Use cin.eof().

      [code=cpp]
      while (!cin.eof())
      {
      //etc...
      }
      [/code]

      eof() is a member function of the base class for istream. You can use eof() with any input stream.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Soneji
        Hello yet again!

        I seem to be having a problem with a logical OR inside a while loop.

        Basically, I've got it taking user input for a string, and I want them to type 'exit' to... well, exit.

        Here's a shorter non-working version of my code:

        [Code=cpp]
        while( str != "exit" || str != "Exit" )
        [/Code]
        Whatever you type your input will be at least unequal to one of those strings so
        that loop will continue. Better make that operator '&&' instead of '||'.

        kind regards,

        Jos

        Comment

        • Soneji
          New Member
          • Apr 2007
          • 36

          #5
          gpraghuram -- I thought about something similar, I thought about taking the input, and converting it all to lowercase...
          But, I couldn't effectively implement that.

          I'm going to work on that tonight though! Thanks!
          // -------------------------------------------------------------------------

          weaknessforcats -- ( I love cats too! My baby-girl is Oreo. She looks a little like a holstein. ( cow ) -- er, color, not shape. LOL).

          Anyway, I tried using eof(), but for the life of me, I couldn't figure out how to implement that in this program either.

          I tried:
          [Code=cpp]
          while( !std::cin.eof() )
          {
          ...
          }
          [/Code]

          But that didn't work. Hmmm, another thing to work on tonight. Thanks anyway!
          I appreciate you taking the time to help out. ( you too gpraghuram! )
          // -------------------------------------------------------------------------

          Finally, Jos, Once again, you have solved my problem.

          Now you get to explain this: I thought the logical AND required both expressions to be true.
          ( cplusplus.com agrees with me. 8 ^ ) )

          In the statement:
          [Code=cpp]
          while( usrInput != "exit" && usrInput != "Exit" )
          [/Code]
          One will always be false, so how does this work?

          Should the logical AND be used in all test expressions like the one above?
          ( I mean, where the same variable is tested, not where two different ones are. )

          Thanks again for the help! Thanks everyone! It's slow, but I'm getting there.

          Lates!

          -Soneji

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by Soneji
            Finally, Jos, Once again, you have solved my problem.

            Now you get to explain this: I thought the logical AND required both expressions to be true.
            ( cplusplus.com agrees with me. 8 ^ ) )

            In the statement:
            [Code=cpp]
            while( usrInput != "exit" && usrInput != "Exit" )
            [/Code]
            One will always be false, so how does this work?

            Should the logical AND be used in all test expressions like the one above?
            ( I mean, where the same variable is tested, not where two different ones are. )

            Thanks again for the help! Thanks everyone! It's slow, but I'm getting there.

            Lates!

            -Soneji
            It will work the way you want because it is saying continue the loop if the string is not either "exit" or "Exit". In other words, if the string is "exit" or "Exit", break the loop. Does that make it clearer?

            Comment

            • inihility
              New Member
              • Jun 2007
              • 18

              #7
              ilikepython is correct.

              Unlike in Discrete Mathematics where AND is true only when both operators are true, and where OR is only false when both operators are false, in C++ loops you have to apply another layer of thought, where when put in simple english:

              Code:
              while ( x !== "Exit" && y !== "exit")
              {
              
              }
              means, While x AND y is false do this.
              so you would only have to prove one operator true to break out of the loop.

              Code:
              while ( x !== "Exit" || y !== "exit")
              {
              
              }
              means, While x OR y is false do this.
              in this case you would have to prove BOTH operators true to break out of the loop.

              So as you can see, you can quite literally "read" the condition in the loop out loud in plain english; it does what it says.

              Hopefully I was some help, I'm still rather new to C++ =)

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by Soneji
                Now you get to explain this: I thought the logical AND required both expressions to be true.
                ( cplusplus.com agrees with me. 8 ^ ) )

                In the statement:
                [Code=cpp]
                while( usrInput != "exit" && usrInput != "Exit" )
                [/Code]
                One will always be false, so how does this work?
                Apply De Morgan's law: you want to exit the loop when the input is either
                "Exit" or "exit"; the following condition should stop the loop

                [code=cpp]
                (x == "Exit" || x == "exit")
                [/code]

                So you want to continue the loop if the above condition is false or when the
                following expression is true:

                [code=cpp]
                !(x == "Exit" || x == "exit")
                [/code]

                The condition above can be rewritten (De Morgan's law) as:

                [code=cpp]
                x != "Exit" && x != "exit"
                [/code]

                kind regards,

                Jos

                Comment

                • Soneji
                  New Member
                  • Apr 2007
                  • 36

                  #9
                  Durn, Jos, you posted before I got my post up. Anyways, here it is:
                  ( Thanks again! )

                  Heh...

                  I really appreciate the help ilikepython, and inihility.

                  I had figured out the answer to my problem, and I was trying to get back on here so I could post this message before anyone else responded, but my cable is all screwed up. ( it drops for no reason. )

                  I was sitting here looking at another piece of code that is made the same way, and it hit me.

                  Both expressions must be true for the loop to continue.
                  However, when one of the expressions are proven false, ( for example, when usrInput DOES equal "Exit" ) then the loop ends.

                  Inihility, I got what you were saying, but you got the wording backwards, the expressions need to be true to continue, and false to end. :)

                  Thanks again for the help everyone. I'm off to continue my learning!

                  Lates, again!

                  -Soneji
                  Last edited by Soneji; Jun 13 '07, 06:42 AM. Reason: Jos posted again before my post was shown... :p

                  Comment

                  • inihility
                    New Member
                    • Jun 2007
                    • 18

                    #10
                    Originally posted by Soneji
                    Inihility, I got what you were saying, but you got the wording backwards, the expressions need to be true to continue, and false to end. :)

                    Thanks again for the help everyone. I'm off to continue my learning!

                    Lates, again!

                    -Soneji
                    Indeed thats what I meant...I guess I was thinking too much into the context of your question (!==) that I typed true instead of false =P

                    Comment

                    Working...