By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,009 Members | 1,487 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,009 IT Pros & Developers. It's quick & easy.

'while' loop with a logical OR.

P: 36
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:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3.  
  4. int main()
  5. {
  6.   std::string str( " " );
  7.  
  8.   std::cout << "Enter some text.  \"exit\" to quit. ";
  9.  
  10.   while( str != "exit" || str != "Exit" )
  11.   {
  12.     std::cin >> str;
  13.     std::cout << "You entered: " << str << "\n";
  14.   }
  15.  
  16.   return 0;
  17. }
  18.  
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
Jun 12 '07 #1
Share this Question
Share on Google+
9 Replies


gpraghuram
Expert 100+
P: 1,275
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
Jun 12 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Use cin.eof().

Expand|Select|Wrap|Line Numbers
  1. while (!cin.eof())
  2. {
  3.    //etc...
  4. }
  5.  
eof() is a member function of the base class for istream. You can use eof() with any input stream.
Jun 12 '07 #3

Expert 10K+
P: 11,448
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:

Expand|Select|Wrap|Line Numbers
  1.   while( str != "exit" || str != "Exit" )
  2.  
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
Jun 12 '07 #4

P: 36
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:
Expand|Select|Wrap|Line Numbers
  1.   while( !std::cin.eof() )
  2.   {
  3.     ...
  4.   }
  5.  
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:
Expand|Select|Wrap|Line Numbers
  1.   while( usrInput != "exit" && usrInput != "Exit" )
  2.  
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
Jun 13 '07 #5

ilikepython
Expert 100+
P: 844
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:
Expand|Select|Wrap|Line Numbers
  1.   while( usrInput != "exit" && usrInput != "Exit" )
  2.  
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?
Jun 13 '07 #6

P: 18
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:

Expand|Select|Wrap|Line Numbers
  1. while ( x !== "Exit" && y !== "exit")
  2. {
  3.  
  4. }
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.

Expand|Select|Wrap|Line Numbers
  1. while ( x !== "Exit" || y !== "exit")
  2. {
  3.  
  4. }
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++ =)
Jun 13 '07 #7

Expert 10K+
P: 11,448
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:
Expand|Select|Wrap|Line Numbers
  1.   while( usrInput != "exit" && usrInput != "Exit" )
  2.  
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

Expand|Select|Wrap|Line Numbers
  1. (x == "Exit" || x == "exit")
  2.  
So you want to continue the loop if the above condition is false or when the
following expression is true:

Expand|Select|Wrap|Line Numbers
  1. !(x == "Exit" || x == "exit")
  2.  
The condition above can be rewritten (De Morgan's law) as:

Expand|Select|Wrap|Line Numbers
  1. x != "Exit" && x != "exit"
  2.  
kind regards,

Jos
Jun 13 '07 #8

P: 36
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
Jun 13 '07 #9

P: 18
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
Jun 13 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.