#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    3
    Rep 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 :D

    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)->

    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 11:52 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    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'))
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    3
    Rep 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?
  6. #4
  7. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    I'm afraid your logic's at fault in this case.

    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/
  8. #5
  9. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    Oh, just for kicks, here's a web site with some info on boolean algebra. Enjoy :)
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    3
    Rep 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.
  12. #7
  13. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    28
    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

IMN logo majestic logo threadwatch logo seochat tools logo