willisj318
willisj318

Reputation: 105

C# prompting for a boolean value

My searches have come up blank, or I am just not understand the results that I am finding. I am trying to prompt the user to input a boolean value, but I want the answer to be yes or no. Let me know if you need to see more code but I have this for the prompt.

public static bool getBoolInputValue(string inputType)
    {
        bool valid = false;
        bool value = true;
        string inputString = string.Empty;
        do
        {
            inputString = GetInput(inputType);
            if (!(String.IsNullOrEmpty(inputString)))
            {
                valid = bool.TryParse(inputString, out value);
            }
            if (!valid)
                Console.WriteLine("Invalid " + inputType + " try again!");
        } while (!valid);

        return value;
    }

This is the paramater for my boolean. Maybe this needs to be more specific?

public bool Nitrus
    {
        set { nitrus = value; }
        get { return nitrus; }
    }

Thank you for the help. I am fairly new to programming but cannot figure this out. It does prompt successfully, but it does not matter what answer I put into the box, it tells me it is not the right format.

Upvotes: 2

Views: 18008

Answers (5)

Hemant Chauhan
Hemant Chauhan

Reputation: 51

main()
{
    bool bIsLifeGood = TakeUserBooleanInput("Is life good? [Y/N]: ");
}

private static bool TakeUserBooleanInput(string inputMsgPrompt)
{
    bool bUserBooleanInput = false;
    bool validInputCollected = false;

    do
    {
        try
        {
            Console.Write(inputMsgPrompt);
            bUserBooleanInput = ParseUserBooleanInput(Console.ReadLine());
            validInputCollected = true;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            validInputCollected = false;
        }

    } while (validInputCollected == false);

    return bUserBooleanInput;
}

private static bool ParseUserBooleanInput(string temp)
{
    bool bReturn = false;
    string tmpLower = temp.ToLower().Trim();
    switch (tmpLower)
    {
        case "yes":
            bReturn = true;
            break;

        case "y":
            bReturn = true;
            break;

        case "true":
            bReturn = true;
            break;

        case "no":
            bReturn = false;
            break;

        case "n":
            bReturn = false;
            break;

        case "false":
            bReturn = false;
            break;

        default:
            bReturn = bool.Parse(tmpLower);
            break;

    }

    return bReturn;
}

Upvotes: 0

devdigital
devdigital

Reputation: 34349

Your program will only accept 'true' or 'false' as valid boolean values. If you want 'yes' or 'no', then you will need to do a string comparison.

valid = string.Compare(inputString, "yes", true) == 0 || string.Compare(inputString, "no", true) == 0;

...
value = string.Compare(inputString, "yes", true) == 0;

Upvotes: 0

jb.
jb.

Reputation: 10341

If I understand correctly you want the user to type in "yes" and that will mean you have a True value. If that is correct, skip all the string.IsNullOrEmpty and bool.TryParse stuff and do something more like this.

//make it ToLower so that it will still match if the user inputs "yEs"
inputString = GetInput(inputType);
if (inputString.ToLower() == "yes")
{
  value = true;
}
else
{
  value = false;
}

//note that the if/else code is the same as directly applying 
// the value from the comparison itself:
// value = inputString.ToLower() == "yes";

// this allows the user to type in "yes" or "y":
// value = inputString.ToLower() == "yes" || inputString.ToLower() == "y";

Upvotes: 2

Phillip Ngan
Phillip Ngan

Reputation: 16106

public static bool getBoolInputValue()
{
    bool value;
    bool valid;
    do
    {
        Console.WriteLine("Enter yes or no: ");
        var inputString = Console.ReadLine();
        if (String.IsNullOrEmpty(inputString))
        {
            continue;
        }
        if ( string.Equals(inputString, "yes")
        {
           value = true;
           valid = true;
        }
        else if ( string.Equals(inputString, "no")
        {
           value = false;
           valid = true;
        }

    } while (!valid);

    return value;
}

Upvotes: 0

Ben Voigt
Ben Voigt

Reputation: 283614

Boolean.TryParse only recognizes "True" and "False". For "Yes" and "No` or "On" and "Off", you need to write your own function.

Upvotes: 0

Related Questions