Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to compare chars in C#

I am new to C# I have started learning it to broaden the programming languages to my disposal but I have run into a little problem I did not encounter in neither C nor Java.

I am trying to get a user response from the keyboard and then comparing it with probable cases and if none of those cases matched up then I want the user to have to repeat the process until he has entered in a correct value.

String input = Console.ReadLine();

while ((input[0] != 'N') || (input[0] != 'Y'))
    input = Console.ReadLine();       
if (input[0] == 'N')
{
    Console.WriteLine("NO");
    Console.ReadKey();
}
else if (input[0] == 'Y')
{
    Console.WriteLine("YES");
    Console.ReadKey();
} 

This is by far not the most efficient way I have tried, i also tried doing a do while loop and many other variants.

The problem I encounter is that when the while loop is not activated everything works just fine, but when I add it in, it always enters the loop even if the input is N or Y and can never leave the loop even though it is clear that it is wrong.

Please if someone can give me some insight as to why this is occurring or if someone may propose a better way of doing this it would be greatly appreciated. Thank you.

Karim

like image 445
JahKnows Avatar asked Dec 22 '12 06:12

JahKnows


People also ask

Can you use == to compare chars?

Yes, char is just like any other primitive type, you can just compare them by == .

Can you compare strings with == in C?

You can't compare strings in C with ==, because the C compiler does not really have a clue about strings beyond a string-literal.

Can you compare string with char in C?

C strcmp()The strcmp() compares two strings character by character. If the strings are equal, the function returns 0.


3 Answers

Right now, the conditional in your while statement will always be true. A good way to test for this is to put a breakpoint where the while loop is being tested, and then "Add watch" to each part of the conditional.

Change

while ((input[0] != 'N') || (input[0] != 'Y'))

to

while ((input[0] != 'N') && (input[0] != 'Y'))
like image 132
Brandon Avatar answered Oct 16 '22 06:10

Brandon


As everyone has already pointed out, you are using || where you should be using &&.

Beyond that, you are also attempting to access the first character of a string that is possible empty. This will cause an exception if the user just hits 'Enter'.

Since you asked for a better way, here's one alternative, that uses Console.ReadKey instead of Console.ReadLine, as you only seem to be interested in getting a character anyway. It also has the advantage that it is not case-sensitive.

while (true)
{
    ConsoleKeyInfo key = Console.ReadKey();
    Console.WriteLine(""); // Just for nice typesetting.

    if (key.Key == ConsoleKey.N)
    {
        Console.WriteLine("NO");
        break;
    }

    if (key.Key == ConsoleKey.Y)
    {
        Console.WriteLine("YES");
        break;
    }
}
like image 28
Ergwun Avatar answered Oct 16 '22 08:10

Ergwun


The problem is in condition checking.

while ((input[0] != 'N') || (input[0] != 'Y'))

Suppose 'N' is entered. Now condition (input[0] != 'Y') becomes false and it should break out of while loop but because of || with (input[0] != 'N') which remains true, the end result in condition comes out to be true and hence it never breaks out of loop.

Replace || with &&

like image 1
Mudassir Hasan Avatar answered Oct 16 '22 06:10

Mudassir Hasan