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
Yes, char is just like any other primitive type, you can just compare them by == .
You can't compare strings in C with ==, because the C compiler does not really have a clue about strings beyond a string-literal.
C strcmp()The strcmp() compares two strings character by character. If the strings are equal, the function returns 0.
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'))
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;
}
}
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 &&
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With