'c# comp sci , bool/if/else statements not working as intended
So im in my first month of class and we just got our 2nd lab for creating a 3 digit code lock in c# console. I am wanting the user to input 3 digits at the same time (and not seperately) and for some reason my code will allow any answer as long as one of the digits is correct
Example: codepassword is 111 and user inputs is 1, it will still use the if statement for it being correct:
int password = int.Parse(Console.ReadLine());
int correct = 111;
bool passwordb = Convert.ToBoolean(password);
bool right = Convert.ToBoolean(correct);
if (passwordb == right)
{
Console.WriteLine("Access Granted");
}
else
{
Console.WriteLine("Access Denied, Try again");
}
Solution 1:[1]
Why all the Convert.ToBoolean? All you need is
int password = int.Parse(Console.ReadLine());
int correct = 111;
if (password == correct)
{
Console.WriteLine("Access Granted");
}
else
{
Console.WriteLine("Access Denied, Try again");
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | pm100 |
