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
  • 1
    switch(optionString.ToLower()) and make all the cases lower case. plus default needs to come after. Commented Jul 28, 2018 at 21:38

3 Answers 3

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Probably better if you just use the first char on the switch case.
@Steve that returns true if the user writes any gibberish starting with "Y" or "N" like "YABCD" would turn the feature on
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 ;)

1 Comment

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)
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.

2 Comments

That would return true if the user writes any gibberish with "y" or "n" in the middle, wouldn't?
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.