|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
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:
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. |
|
#2
|
|||
|
|||
|
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')) |
|
#3
|
|||
|
|||
|
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? |
|
#4
|
||||
|
||||
|
I'm afraid your logic's at fault in this case.
Quote:
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/ |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Problem using "or" in a while loop |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|