Thread: question about ||

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    26

    question about ||

    ok i can't seem to figure out what is goin on here. i'm using or in my while statement and an if statement further down and no matter what i type in i can't get the while statement to leave. even if i type n or N. any help would be great

    thanks, rubiks14

    Code:
    char stayChoice = 'r';
    
    while((stayChoice != 'n') || (stayChoice != 'N'))
    {
    	cout << "Are you sure you want to stay at the Inn? (y/n)\n";
    	cout << endl;
    	cout << ": ";
    	cin >> stayChoice;
    	cin.ignore();
    	cout << endl;
    
    	if((stayChoice == 'y') || (stayChoice == 'Y'))
    	{
    		if(player.gold >= 50)
    		{
    			cout << "Your HP/MP have been restored\n";
    			SetHealth(player);
    			SetMana(player);
    			player.gold -= 50;
    			cout << "Health: " << player.health << "/" << player.maxHealth << endl;
    			cout << "Mana: " << player.mana << "/" << player.maxMana << endl;
    		}
    		else
    		{
    			cout << "Sorry you don't have enough gold.\n";
    			break;
    		}
    	}//if(stayChoice == 'y' || stayChoice == 'Y')
    }//while(stayChoice != 'n' || stayChoice != 'N')

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Look at your loop it proclaims that if the variable doesn't equal 'N' or doesn't equal 'n' it should loop. Which means, in order to exit the loop, the variable would have to equal both 'N' AND 'n' in order to exit. That's impossible. What you want to use in that loop is && not ||
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    For an OR to be true, only one conditional has to be true. If you enter 'n', will this be true or false:

    stayChoice != 'N'

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    slymaelstrom it works but i thought && was AND and that || was OR. so i am sorta confused here. :P

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    It's pretty simple, how many of the conditionals have to be true in an OR statement for the whole thing to be true?

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Think of it this way... if I were to say to you "Should I punch you or should I slap you", how would you tell me you don't want either of those to happen?

    Would you say "I don't want you to punch me OR I don't want you to slap me"
    or would you say "I don't want you to punch me AND I don't want you to slap me"?

    Note: This is hypothetical, I'm not interested in punching you or slapping you.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    just one. but in an AND statement both have to be

    good way of puttin it slymaelstrom...and they say violence doesn't solve anything .

  8. #8
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Code:
    while((stayChoice != 'n') || (stayChoice != 'N'))
    If stayChoice does not equal 'n' or does not equal 'N' then begin. Thats what your loop says.

    This looks strange.

    Code:
    char stayChoice = 'r';
    Its perfectly legal, but It can cause issues. Try;

    Code:
    char stayChoice[1] = 'r';
    That way one space is allocated to the char. Oh, and you need to change all your calls to stayChoice to;

    Code:
    if(stayChoice[0] == 'n' || stayChoice[0] == 'N')
     DoStuff();
    That shouldent be an actual problem though.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    but in an AND statement both have to be
    So if you have this statement:
    Code:
    (stayChoice != 'n') && (stayChoice != 'N')
    and stayChoice is equal to 'y', how many of those conditionals are true? And, is the whole thing true or false?

    If stay choice is 'n', how many of the conditionals are true? And, is the whole thing true or false?

    If stay choice is 'N', how many of the conditionals are true? And, is the whole thing true or false?

    Directly translating English language into code doesn't work. You usually have to make a guess at whether it should be AND or OR, but then to make sure you are right, you have to ask yourself the above questions.
    Last edited by 7stud; 02-21-2006 at 10:58 PM.

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by 7stud
    Directly translating English language into code doesn't work. You usually have to make a guess at whether it should be AND or OR, but then to make sure you are right, you have to ask yourself the above questions.
    "I don't want stayChoice to be 'n' and I don't want stayChoice to be 'N'."

    No?

    Now, converting to a speakable language could cause problems if you don't say it phonetically. Take, for example:

    "I don't want to be punched and I don't want to be slapped" versus
    "I don't want to be punched and slapped"

    They both mean completely different things, especially when converted into code, but could arguable be understood the same in conversation if one weren't to nitpick.
    Last edited by SlyMaelstrom; 02-21-2006 at 11:15 PM.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe Program Question
    By Unknowntoyou000 in forum C++ Programming
    Replies: 24
    Last Post: 04-10-2008, 08:55 PM
  2. Hide the cursor???
    By Smoki in forum C++ Programming
    Replies: 28
    Last Post: 01-28-2006, 01:55 PM
  3. Question about restarting a loop
    By librab103 in forum C Programming
    Replies: 10
    Last Post: 07-19-2003, 12:31 PM
  4. Shorter notation?
    By GaPe in forum C Programming
    Replies: 2
    Last Post: 06-14-2003, 11:35 AM
  5. Compiling in Unix vs Visual C++
    By stimpyzu in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2002, 06:41 AM