none
absolute beginner stuck! RRS feed

  • Question

  • I've been trying to learn coding and chose c# to begin with. I'm trying to make a text based game but seem stuck.

    when I run the code, the error message is displayed even when the conditions have been met. can someone explain what it is I'm doing wrong?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace rpg2
    {
        class Program
        {
            static void Main(string[] args)
            {
                //ints:
                int correct = 0;

                //strings:
                //Character creation:
                do
                {
                    Console.Write("Are you male or female?: ");
                    string gender = Console.ReadLine();
                    gender = gender.ToUpper();
                    //used this to make sure input would convert to uppercase
                    /*
                    Console.WriteLine(gender);
                    Console.ReadLine();
                    */
                 
                    if (gender == "MALE")
                    {
                        correct = 1;
                    }
                    if (gender == "FEMALE")
                    {
                        correct = 1;
                    }
                    else if (gender != "MALE" || gender != "FEMALE")
                    {
                        Console.WriteLine("ERROR. Did not understand the answer.");
                        Console.ReadLine();
                    }
                } while (correct == 0);
                correct = 0;
            }
        }
    }

    Sunday, January 21, 2018 3:44 AM

All replies

  • Hello,

     When posting code please use code blocks and you did not state what the problem was.

    However, after reviewing your code logic, the problem is with the

     else if ( gender != "MALE" || gender != "FEMALE" )

     when the conditionals are evaluated they are OR ed together.

    Because you are checking for Not Equal, a True result means the

    variable is not what you wanted.  If either are True and the

    other is False you get results of True. Because of the OR and the

    enclosing brackets code executes even when variable gender is

    correct value.  Try replacing the || with && which is the AND operator.

    Should fix the issue.

     

     Hope this helps :)

    • Proposed as answer by Cor Ligthert Sunday, January 21, 2018 9:58 AM
    Sunday, January 21, 2018 4:17 AM
  • Thanks a lot!. and thanks for the advice, I'm really new to this and specially asking for help. I will keep that in mind for future questions. 
    Sunday, January 21, 2018 9:07 AM
  • Hello Jose,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Thank you for your understanding and cooperation.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, January 28, 2018 1:21 PM
    Moderator