Thread: An infintely looping while loop and the quandry of != relational operators

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    36

    An infintely looping while loop and the quandry of != relational operators

    Okay, so the time has come for me to correct a hole in my knowledge about my while loops, namely, why they never seem to be able to check for my != relational operators.

    So I was writing a loop for the inventory function of my text adventure when I realized that the best thing I could use to check to see if the player had input certain integers was the != operator. I wrote it like so:

    Code:
      while (inventory_choice_reference != 1 || inventory_choice_reference != 2 || inventory_choice_reference !=9)
            {
                cout << "You are currently carrying: \n";
                if (healing_potion_reference > 0)
                {
                    cout << "1. Healing Potion (Restores player to full health (10)) \n";
                }
                if (half_healing_potion_reference > 0)
                {
                    cout << "2. Half Healing Potion (gain 3 health)\n";
                }
                cout << "Enter the number of the item you'd like to use, or type 9 to exit: ";
                cin >> inventory_choice_reference;
                if (inventory_choice_reference == 1 && healing_potion_reference == 0)
                {
                    cout << "You don't have one of those.\n";
                    inventory_choice_reference = 0;
                }
                if (inventory_choice_reference == 2 && half_healing_potion_reference == 0)
                {
                    cout << "You don't have one of those.\n";
                    inventory_choice_reference = 0;
                }
            }
    This loops even if the player inputs 9 as the inventory_choice_reference, but I know that inventory_choice_reference is being modified properly because the "You don't have one of those." statements show up fine when you enter 1 or 2, but not 9.

    I've had problems like this when putting != statements as loop conditions before. So much so that I usually define loops around case switches a bit like this:

    Code:
    while (player_choices.tutorial_choice_two > 2 || player_choices.tutorial_choice_two < 1)
                {
                cout << "Choose an option: \n";
                cin >> player_choices.tutorial_choice_two;
                cin.ignore();
            switch (player_choices.tutorial_choice_two)
                {
                case 1:
                    breathe_deep (player_race_reference, player_health_stat_reference, location_reference, healing_potion_reference, half_healing_potion_reference, inventory_choice_reference);
                    cout << "You open your eyes. /n/nThe mare gives you a nod. \"Good work, your heart rate is down allready.\"\n\n";
                break;
                case 2:
                    cout<< "The mare sighs, \"Okay, I'll admit, it DOES seem a bit silly, and it's not like you have a lot of reason to trust me...\nStill, if you ever feel more comfortable with it, I'd reccomend giving it a try.\"\n\n";
                break;
                }
                }
    To avoid having to deal with using != even though I know some user might type in 1.5 some day and blow up their computer.

    Can anyone tell me what I'm doing wrong here? I could really use the help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while (inventory_choice_reference != 1 || inventory_choice_reference != 2 || inventory_choice_reference !=9)
    Two cases
    choice == 1, evaluates to 1 != 1 || 1 != 2 || 1 != 9 (false || true || true is true)
    choice == 99, evaluates to 99 != 1 || 99 != 2 || 99 != 9 (true || true || true is true)

    Your loop can never exit.


    Perhaps
    while (inventory_choice_reference == 1 || inventory_choice_reference == 2 || inventory_choice_reference ==9)
    which will loop until you type in something other than 1, 2 or 9
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    36
    Oh now I get it! Thank you so much Salem!
    I fixed the whole thing by making the ||s into &&s. I really need to brush up on basic true and false.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    When I see something like this:

    while (inventory_choice_reference != 1 || inventory_choice_reference != 2 || inventory_choice_reference !=9)

    Then I tend to translate it into inverse logic so we get the condition for when the loop stops.
    So that is equivalent with

    while ( !(inventory_choice_reference == 1 && inventory_choice_reference == 2 && inventory_choice_reference == 9) )

    which makes no sense.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what' value relational operators returned
    By liyanhong in forum C Programming
    Replies: 10
    Last Post: 06-15-2010, 04:27 AM
  2. problem with relational operators
    By manoncampus in forum C++ Programming
    Replies: 10
    Last Post: 12-13-2005, 07:15 PM
  3. Relational Operators
    By Surfin_Bird in forum C Programming
    Replies: 9
    Last Post: 01-16-2005, 07:45 PM
  4. problem with relational operators
    By Sargnagel in forum C Programming
    Replies: 4
    Last Post: 10-13-2002, 09:45 AM
  5. Relational and Logical Operators
    By GSLR in forum C Programming
    Replies: 2
    Last Post: 03-03-2002, 04:33 PM