Reputation: 59
I'm working on a simple password storage console application in c#.
I'm having a bit of a problem on a section that asks if the user would like to mask all password entries from that point on.
This is the section of code:
bool tryagain = true;
while(tryagain == true)
{
Console.WriteLine("Would you like to mask all other password entiries?(Y,N)");
string input = Console.ReadLine();
if (input == "y" | input == "Y")
//Something wrong, always passes to Your awnser was invalid
{
maskpass = true;
tryagain = false;
}
if (input == "n" | input == "N")
{
maskpass = false;
tryagain = false;
}
else
{
Console.WriteLine("Your awnser was invalid, would you like to try again?");
string yesno = Console.ReadLine();
if (yesno == "y" | yesno == "Y")
{
tryagain = true;
}
if (yesno == "n" | yesno == "N")
{
Environment.Exit(0);
}
}
}
The problem is when I run the code it always runs to the else statement.
I'm certain the mistake is very simple and I'm just being ignorant but anyone have any idea on whats going on here?
Upvotes: 0
Views: 231
Reputation: 29
You can either use || or you can use the method of the String class String.equals. Since it is a String you are reading as input better use the String.equals method
Upvotes: 0
Reputation: 62266
Use ||
instead of single |
. The ||
mean or conditional, but single |
is binary or.
I assume that the logic of your code says:
if input=='y' OR input=="Y"
, do something.
Another suggession yet. If my assumption right, you can achiev that with simple String.Equals overload:
if(input.Equals("y", StringComparison.InvariantCultureIgnoreCase)
{
//do something/
}
Upvotes: 7