The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Problem using "or" in a while loop
Discuss Problem using "or" in a while loop in the C Programming forum on Dev Shed. Problem using "or" in a while loop C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 8th, 2002, 10:45 PM
|
|
Junior Member
|
|
Join Date: Nov 2002
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Problem using "or" in a while loop
Hello!
I'm a student taking C++, fun stuff. I hope to one day get good at this stuff, I sorta am already, but good at the elementary levels
Here's my problem; I'm writing a program to flip a coin x amount of times and print the results. Of course I want to go one step higher and ask the user if he would like to redo the "experiment," so there's the simpe "Do you want to to it again? (y/n)" prompt. My problem is this- I want to use both cases (n and N) to say "no," and the or thingymabobber (||) isn't working for me. Here's the chunk of code that isn't being nice to me (There's more to it, but its irrelevant because it works, well most of this code is irrelevant, but I just copied and pasted)->
Quote: int holder, heads, tails;
char cont;
while (cont != 'n') || (cont != 'N') //<-- Problem THERE
{
holder = 0; //Holds the temporary value in the for loop
heads = 0; //Number of heads (zeros) returned
tails = 0; //Number of tails (ones) returned
for (int count = getcount(); count >= 0; count--)
{
holder = flipcoin();
if (holder == 0)
heads = heads + 1;
else
tails = tails + 1;
}
printresults (heads, tails);
cout << "Do you want to do it again? (y/n) ";
cin >> cont; |
I've tried everything with that while (), but I bet it's a simple fix. So-- what did I screw up this time?
Thanks a lot.
EDIT-
Perhaps I should tell you what went wrong too. Silly me.
When I tried compiling it as is it gave me a parse error before the ||
When I had-
while (cont != 'n' || cont != 'N')
- The program compiled and ran, but did not recognize the check. Well, it asked for input, but if I entered 'n' or 'N', the program breezed right through it and repeated itself. Same thing with-
while (cont != 'n' || 'N')
Last edited by beefstu01 : November 8th, 2002 at 10:52 PM.
|

November 9th, 2002, 02:35 AM
|
|
Contributing User
|
|
Join Date: Jan 2002
Location: Seattle WA
Posts: 863
  
Time spent in forums: 22 sec
Reputation Power: 13
|
|
You almost got it. You just need to add nested parentheses, so that each condition is evaluated, then the OR of each evaluation is taken.
Code:
while ((cont != 'n') || (cont != 'N'))
|

November 9th, 2002, 10:04 AM
|
|
Junior Member
|
|
Join Date: Nov 2002
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Hrm.. that's odd. When I do that, the program compiles but still doesn't recognize the 'n' or 'N'
Any idea what the problem could be, or is it just my compiler being dumb?
|

November 9th, 2002, 12:01 PM
|
 |
*bounce*
|
|
Join Date: Jan 2002
Location: Delft, The Netherlands
|
|
I'm afraid your logic's at fault in this case.
Quote: | while ((cont != 'n') || (cont != 'N')) |
As you know, a while-loop loops as long as the conditional expression is True. So when cont is either 'n' or 'N', you want the expression to evaluate to False, right?
Let's assume cont contains the value of 'n', and see what happens:
a) cont != 'n' evaluates to False, clearly enough.
b) cont != 'N' evaluates to True however, since 'n' != 'N'.
c) The logical OR of True and False evaluates to True, so the conditional expression holds. The while-loop continues looping.
That's applying the definitions, but if you just think about the whole expression for a bit (you know, let it sink in), you'll find that it indeed doesn't make sense.
What you -really- want is the expression
Code:
while ((cont != 'n') && (cont !='N'))
Of course you could also apply De Morgan's rule (at least, one of them), and come up with the equivalent
Code:
while (!(cont == 'n' || cont == 'N'))
Hopefully this will clear things up a little 
__________________
"A poor programmer is he who blames his tools."
http://analyser.oli.tudelft.nl/
|

November 9th, 2002, 12:05 PM
|
 |
*bounce*
|
|
Join Date: Jan 2002
Location: Delft, The Netherlands
|
|
Oh, just for kicks, here's a web site with some info on boolean algebra. Enjoy 
|

November 9th, 2002, 12:52 PM
|
|
Junior Member
|
|
Join Date: Nov 2002
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Oh my God, how could I be so stupid!
Duh, thanks a whole bunch. Course II math and all that logic stuff flew out of my head. If you can imagine the sound of a head banging on a desk, well, thats the sound I'm making.
Thanks sooooo much. You rock.
|

November 10th, 2002, 11:13 PM
|
|
Offensive Member
|
|
Join Date: Oct 2002
Location: in the perfect world
|
|
You are forgetting to initialise the while condition variable
char cont; //could start = 'n'
use
toupper(cont)
then just test for 'N'
can streamline somewhat
Code:
if(FlipCoin())
heads++;
else
tails++;
__________________
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.
Frank Zappa
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|