Like Tree1Likes
  • 1 Post By VirtualAce

Issue with if statement + a general question

This is a discussion on Issue with if statement + a general question within the Game Programming forums, part of the General Programming Boards category; Hello! I just started working on my first proper C++ project, and have finally run into an issue I haven't ...

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    2

    Issue with if statement + a general question

    Hello! I just started working on my first proper C++ project, and have finally run into an issue I haven't been able to solve myself. I've spent a few hours thinking about it and fiddling around with it while searching to see if I could find a comparable example, but no dice, so here I am.

    The issue is here:
    Code:
    //Get player's gender
    string playerGenderFunct()
    {
        enum playerGender{MALE, FEMALE, NEUTRAL};
        cout << "What pronouns do you prefer?" << endl;
        cout << "Select from the following:" << endl;
        cout << "Male" << endl;
        cout << "Female" << endl;
        cout << "Neutral (ze/hir)" << endl;
        map <string, playerGender> playerGenderMap;
        playerGenderMap["Male"] = MALE;
        playerGenderMap["Female"] = FEMALE;
        playerGenderMap["Neutral"] = NEUTRAL;
        string selectedPlayerGender;
        cin >> selectedPlayerGender;
        playerGender playerPronouns = playerGenderMap[selectedPlayerGender];
        if (selectedPlayerGender != "Male" || selectedPlayerGender != "Female" || selectedPlayerGender != "Neutral")
        {
            cout << "That is not a supported option, please re-enter." << endl;
            cin >> selectedPlayerGender;
        }
        cout << "You prefer " << selectedPlayerGender << " pronouns.  Is this correct? If so, hit 'y'." << endl;
        char playerGenderCheck = 'n';
        cin >> playerGenderCheck;
        do
        {
            if (playerGenderCheck != 'y')
            {
                cout << "Please re-enter your gender." << endl;
                cin >> selectedPlayerGender;
                if (selectedPlayerGender != "Male" || selectedPlayerGender != "Female" || selectedPlayerGender != "Neutral")
                {
                    cout << "That is not a supported option, please re-enter." << endl;
                    cin >> selectedPlayerGender;
                }
                cout << "Are " << selectedPlayerGender << " pronouns your preference? If so, hit 'y'." << endl;
                cin >> playerGenderCheck;
            }
        } while (playerGenderCheck != 'y');
        cout << "\n\n";
    
        return selectedPlayerGender;
    }
    What it is meant to do is ask a question with preset options, check to see if it fits one of the options, and if not ask for it to be re-entered. However it currently has an issue where no matter what is entered the first time, it says it's incorrect. I'm figuring that the issue is with the first batch of this code.

    Code:
        if (selectedPlayerGender != "Male" || selectedPlayerGender != "Female" || selectedPlayerGender != "Neutral")
        {
            cout << "That is not a supported option, please re-enter." << endl;
            cin >> selectedPlayerGender;
        }
    I've tried a few different things, from moving it around a bit (since the second time that code shows up it appears to be working fine?) to changing it to do and while loops and sticking continue in there...nothing has worked. So I'd really love some suggestions on things to try to get it to work, since this appears several times in the program. Alternatively if you have a suggestion of a better way to accomplish this I'd be open to that too.

    I'm also just trying to get into good habits, so if you see something else blaringly awkward about the code feel free to let me know.

    Nevermind on the following question, I finally figured out what it was called.

    I'm also wondering how you you would go about attaching a script (plot/speech, not code) to a program, as I'm assuming it generally isn't written straight into the source code? I've been having some trouble searching for an answer to this since script tends to mean code when talking about programming. (If there is some more appropriate term for what I'm trying to figure out you could just tell me that and I can probably find what I'm looking for on my own if you don't feel like typing up a long response.)

    Thank you!
    Last edited by Rumtehk_uh; 08-21-2013 at 02:30 PM. Reason: Answered question, specified C++

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    > if (selectedPlayerGender != "Male" || selectedPlayerGender != "Female" || selectedPlayerGender != "Neutral")
    Think about it.
    If selectedPlayerGender is Male, then you have if ( false || true || true )

    There is no value you can assign which would make all three terms false at the same time.

    Your boolean logic is broken.

    Try something like

    if ( !(selectedPlayerGender == "Male" || selectedPlayerGender == "Female" || selectedPlayerGender == "Neutral" ) )
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    2
    That explains it, thank you. The way you have written it allows for it to move on as long as all of them aren't false, correct? (Trying to make sure I understand how the ! works in different situations.)

    I entered it and tested it and it does now allow it to be correct on the first try, but when going through and seeing how it handled it being incorrect I discovered that it will accept entries that it shouldn't as being correct every other time one is entered, and interpret correct ones as incorrect on the other time. (IE every other time it indiscriminately accepts and indiscriminately rejects.) The old code is also doing this and I just hadn't noticed. (I'm betting this is probably why I missed the logic error, since whenever I entered it the second time it was going through.) Any guesses why?
    Last edited by Rumtehk_uh; 08-21-2013 at 04:22 PM. Reason: Clarity of new issue

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    That operator does not work differently in different situations. It always means not in all cases. Salem's example should work, however, I prefer to work in the positive since I find it easier to read. If you change the or to and the condition works.

    Code:
    if (selectedPlayerGender != "Male" && selectedPlayerGender !="Female" && selectedPlayerGender !="Neutral")
    {
        // Player gender is not male, female or neutral
    }
    Example with gender as "Foo".
    1. Gender is not equal to male so evaluation continues.
    2. Gender is not equal to female so evaluation continues.
    3. Gender is not equal to Neutral so evaluation is complete
    4. All conditions satisfied
    5. Improper gender was input.

    Example with gender as "Male".
    1. Gender is equal to male so evaluation is short circuited and stops.
    2. Improper gender was not input.

    Example with gender as "Female".
    1. Gender is not equal to male so evaluation continues.
    2. Gender is equal to female so evaluation is short circuited and stops.
    3. Improper gender was not input.

    Example with gender as "Neutral".
    1. Gender is not equal to male so evaluation continues.
    2. Gender is not equal to female so evaluation continues.
    3. Gender is equal to neutral so evaluation stops but is not short circuited since neutral is the last condition
    3. Improper gender was not input.

    An easier way is to assign a number to each gender choice and have the player enter the number of the choice. That boils down to a simple integer range check and results in simpler logic.
    Last edited by VirtualAce; 08-24-2013 at 04:11 AM.
    Rumtehk_uh likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. general question
    By ingeniousreader in forum C Programming
    Replies: 4
    Last Post: 05-21-2012, 10:59 AM
  2. While Statement Issue.
    By mcertini in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2011, 12:34 PM
  3. Just a general question;
    By fthmdg in forum C++ Programming
    Replies: 7
    Last Post: 04-09-2010, 05:14 PM
  4. issue with switch statement
    By bluetxxth in forum C Programming
    Replies: 14
    Last Post: 02-24-2010, 10:01 AM
  5. General GUI question in C
    By Music_Man in forum Game Programming
    Replies: 3
    Last Post: 11-16-2001, 11:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21