0

I'm writing a code where the user has to input if he wants a feature or not by pressing "y" or "n" or writting "yes" or "no", without checking for case-sensitivity (so it needs to work if the user writes yES or YeS).

Also, if the user writes anything invalid, it says that this option is invalid, and asks again for the user to select an option.

Here's an abstraction of it:

    static bool FeatureIsOn { get; set;}

    static void Main()
    {
        bool optionIsValid;

        do //Loops around until the option is valid
        {
            Console.WriteLine();
            Console.Write("Enable Feature? [Y/N]: ");
            string optionString =  Console.ReadLine();

            switch(optionString)
            {
                default:
                    Console.WriteLine("Invalid option.");
                    optionIsValid = false;
                break;

                case "Yes":
                case "yes":
                case "Y":
                case "y":
                    FeatureIsOn = true;
                    optionIsValid = true;
                break;

                case "No":
                case "no":
                case "N":
                case "n":
                    FeatureIsOn = false;
                    optionIsValid = true;
                break;

            }
        } while (optionIsValid != true);
    }

It's not very efficient to have a case for each possible way to write "yes". Is there a better way to do it?

  • 1
    `switch(optionString.ToLower())` and make all the cases lower case. plus default needs to come after. – Nkosi Jul 28 '18 at 21:38

3 Answers3

4

Convert the string you want to check to uppercase or lowercase before checking:

static bool FeatureIsOn { get; set;}

static void Main()
{
    bool optionIsValid;

    do //Loops around until the option is valid
    {
        Console.WriteLine();
        Console.Write("Enable Feature? [Y/N]: ");
        string optionString =  Console.ReadLine();

        // convert string to uppercase 
        optionString = optionString.ToUpper();

        switch(optionString)
        {
            case "YES":
            case "Y":
                FeatureIsOn = true;
                optionIsValid = true;
            break;

            case "NO":
            case "N":
                FeatureIsOn = false;
                optionIsValid = true;
            break;

            default:
                Console.WriteLine("Invalid option.");
                optionIsValid = false;
            break;

        }
    } while (optionIsValid != true);
}
oRole
  • 1,316
  • 1
  • 8
  • 24
1

You can use methods like String.ToUpper() and then compare the entered string with YES, NO, Y, N (works also with String.ToLower() but I'm not sure it'll be faster.

Otherwise, maybe some if/else would give a cleaner result but it won't change anything in term of speed.

EDIT : An other option would be to use Console.Read() instead of Console.Readline() so if the user wanna type 'YES' it will only keep the 'Y' (it only record one char) which allow you to divide your tests by 2 ;)

Nhqml
  • 88
  • 7
  • At first I compared the first char of the string like `if(optionString[0] == Y)` but I figured that would return true if the user wrote (yno), so now I'm doing it like `if(optionString == YES || optionString == Y)` – Thales Vilela Jul 28 '18 at 22:00
0

Do a trim then a tolower, then validate if it contains a valid option( n,y or no, yes) and then to find out what was the actual choice simply check using .contains for a y. This would be my strategy.

Robbert Draaisma
  • 463
  • 4
  • 14
  • 1
    That would return true if the user writes any gibberish with "y" or "n" in the middle, wouldn't? – Thales Vilela Jul 28 '18 at 21:55
  • No, if you read carefully you’ll see I suggest to first validate the input and then determine the intent. at that stage only a yes or y would be possible match – Robbert Draaisma Jul 28 '18 at 22:16