Richard Morris
Richard Morris

Reputation: 29

How to check user input with if command

Not sure if I'm overlooking something really simple but I'm trying to make a program that allows a user to enter 1 of 2 letters and then run code based on the input. Seems simple enough but I've run into several errors with all the ways I thought this could work. Here is the code:

string name = (Console.ReadLine());
Console.WriteLine("Is " + name + " ok?");
Console.WriteLine("\n(Y)es\n(N)o");
char ansys = Console.ReadKey();
if (ansys = ConsoleKey.Y)
    Console.Clear();
else
{
    Console.WriteLine();
    Console.WriteLine("Enter letters only");
}

I added in the else portion (unfinished)just to get an idea if If i'm going the right direction with the intended goal as well. Would I be able to make an else statement that triggers if neither Y or N is pressed this way?

Upvotes: 0

Views: 10376

Answers (4)

Well, first of all, you are making an assignment, not comparing:

if (ansys.Key = ConsoleKey.Y)

is wrong, use:

if (ansys.Key == ConsoleKey.X)

== is comparison, = is assignment. Don't confuse them, it may cause serious problems.

For you question, if you simply add an else if statement checking for "No" answer, then else statement won't be triggered if Y or N is pressed. If at least if statement is executed, else statement won't be executed.

Your code should look like:

if (ansys == ConsoleKey.Y) {
    // code if yes
}
else if (ansys == ConsoleKey.N) {
    // code if no
}
else {
    // code if neither
}

Edit: Since my primary language is not C#, I looked at documentation to check my answer. I figured out that if you use ReadKey() it does not return a ConsoleKey, it returns struct ConsoleKeyInfo. You need to use Key member of the ConsoleKeyInfo to access the pressed key. Please re-check the code.

Upvotes: 1

Tomas Ceruti
Tomas Ceruti

Reputation: 136

Try this (couldnt test it)

  • This will ask for the name until the confirmation answer is Y
  • If the input when asked Y or N is another thing, it will ask again for the name confirmation.

    string name = "";
    while (name.equals(""))
    {
        name = (Console.ReadLine());
        Console.WriteLine("Is " + name + " ok?");
    
        String answer = "";
        while(answer.equals(""))
        {
            Console.WriteLine("\n(Y)es\n(N)o");
            char ansys = Console.ReadKey();
            if (ansys == ConsoleKey.Y || ansys == ConsoleKey.N)
            {
                answer = ansys.ToString();
                Console.Clear();
            }
            else
            {
                Console.WriteLine();
                Console.WriteLine("Enter letters only!!");
            }
        }
        if(!answer.equals("Y"))
            name = "";
    }
    

Im not sure if ansys.ToString() is a valid method, and if that returns the "Y" string in case the key pressed was Y

Upvotes: 0

Yacoub Massad
Yacoub Massad

Reputation: 27861

Try this:

string name = (Console.ReadLine());
Console.WriteLine("Is " + name + " ok?");
Console.WriteLine("\n(Y)es\n(N)o");
var ansys = Console.ReadKey();
if (ansys.KeyChar == 'y' || ansys.KeyChar == 'Y')
{
    //Handle yes case
}
if (ansys.KeyChar == 'n' || ansys.KeyChar == 'N')
{
    //Handle no case
}
else
{
    Console.WriteLine();
    Console.WriteLine("Enter letters only");
}

Upvotes: 0

Mateusz Kleinert
Mateusz Kleinert

Reputation: 1376

Try this approach:

ConsoleKeyInfo cki;
cki = Console.ReadKey();

if (cki.Key == ConsoleKey.Y)
{
    Console.Clear();
}
else if (cki.Key == Console.N)
{
    Console.Clear();
}
else
{
    Console.WriteLine();
    Console.WriteLine("Enter letters only");
}

You can find th examples here: ReadKey - examples

Upvotes: 0

Related Questions