C
C#2y ago
baykkz

❔ Help with a bool not working

Guys I feel like I'm going crazy. I'm making this Password Validator and for some reason the "no T" rule doesn't work (yes it's a very specific rule, it's a test in a book).
while (true)
{
Console.WriteLine("Enter a password:");
PasswordValidator password = new PasswordValidator(Console.ReadLine());
password.CheckValidity();
}

class PasswordValidator
{
public string Password { get; set; }
public bool HasUpper { get; set; } = false;
public bool HasLower { get; set; } = false;
public bool HasNumber { get; set; } = false;
public bool HasAmpersand { get; set; } = false;
public bool HasT { get; set; } = false;

public PasswordValidator(string password)
{
Password = password ?? "x";
}

public void CheckValidity()
{
int charNumber = 0;
foreach (char letter in Password)
{
charNumber++;
if (char.IsUpper(letter)) HasUpper = true;
else if (char.IsLower(letter)) HasLower = true;
else if (char.IsNumber(letter)) HasNumber = true;
else if (letter == '&') HasAmpersand = true;
else if (letter == 'T') HasT = true;
}

if (HasUpper && HasLower && HasNumber && !HasAmpersand && !HasT &&
charNumber >= 6 && charNumber <= 13) Console.WriteLine("This password is valid!");

else
{
Console.WriteLine("This password is not valid!");
Console.Write("Not detected:");
if (!HasUpper) Console.Write(" UpperCase");
if (!HasLower) Console.Write(" LowerCase");
if (!HasNumber) Console.Write(" Number");
if (HasT) Console.Write(" T absence");
if (HasAmpersand) Console.Write(" Ampersand absence");
if (charNumber < 6 || charNumber > 13) Console.Write(" Correct amount of characters");
Console.WriteLine("");
}
Console.WriteLine("");
}
}
while (true)
{
Console.WriteLine("Enter a password:");
PasswordValidator password = new PasswordValidator(Console.ReadLine());
password.CheckValidity();
}

class PasswordValidator
{
public string Password { get; set; }
public bool HasUpper { get; set; } = false;
public bool HasLower { get; set; } = false;
public bool HasNumber { get; set; } = false;
public bool HasAmpersand { get; set; } = false;
public bool HasT { get; set; } = false;

public PasswordValidator(string password)
{
Password = password ?? "x";
}

public void CheckValidity()
{
int charNumber = 0;
foreach (char letter in Password)
{
charNumber++;
if (char.IsUpper(letter)) HasUpper = true;
else if (char.IsLower(letter)) HasLower = true;
else if (char.IsNumber(letter)) HasNumber = true;
else if (letter == '&') HasAmpersand = true;
else if (letter == 'T') HasT = true;
}

if (HasUpper && HasLower && HasNumber && !HasAmpersand && !HasT &&
charNumber >= 6 && charNumber <= 13) Console.WriteLine("This password is valid!");

else
{
Console.WriteLine("This password is not valid!");
Console.Write("Not detected:");
if (!HasUpper) Console.Write(" UpperCase");
if (!HasLower) Console.Write(" LowerCase");
if (!HasNumber) Console.Write(" Number");
if (HasT) Console.Write(" T absence");
if (HasAmpersand) Console.Write(" Ampersand absence");
if (charNumber < 6 || charNumber > 13) Console.Write(" Correct amount of characters");
Console.WriteLine("");
}
Console.WriteLine("");
}
}
31 Replies
baykkz
baykkzOP2y ago
the "no &" is the same thing but it works
Angius
Angius2y ago
aTy password a -> sets hasT to false T -> sets hasT to true y -> sets hasT to false
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
bool hasX = false;
foreach (var ch in "baXuy") {
hasX = ch == 'X';
Console.WriteLine(hasX);
}
bool hasX = false;
foreach (var ch in "baXuy") {
hasX = ch == 'X';
Console.WriteLine(hasX);
}
Console Output
False
False
True
False
False
False
False
True
False
False
Compile: 574.017ms | Execution: 47.629ms | React with ❌ to remove this embed.
Angius
Angius2y ago
You'd be better off using string.Contains()
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
"baXuy".Contains('X')
"baXuy".Contains('X')
Result: bool
True
True
Compile: 405.034ms | Execution: 24.659ms | React with ❌ to remove this embed.
baykkz
baykkzOP2y ago
but I never make HasT true again after it becomes false there isn't a code for making it true and I'm supposed to use char.IsStuff for the test
Angius
Angius2y ago
Ah, huh, you're right My bad
baykkz
baykkzOP2y ago
That's ok If that were the case the & rule wouldn't work as well
Angius
Angius2y ago
I guess fire up the debugger
baykkz
baykkzOP2y ago
That's what's driving me mad, they are pratically the same thing I didn't get to this part of the book I'm studying
Angius
Angius2y ago
$debug
MODiX
MODiX2y ago
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
baykkz
baykkzOP2y ago
no idea what a debugger does lol huh thanks will take a look
Angius
Angius2y ago
Lets you take a look at the values of variables during the runtime of the program You can pause the execution, and then execute the code line by line, all the while seeing what value each variable has
baykkz
baykkzOP2y ago
oh, that's awesome
Angius
Angius2y ago
Ah I see what's going on You print T absence when HasT is true So you print the error about T not being there... if the T is there Your && set works, because you negate it && !HasT -> "and doesn't have T"
baykkz
baykkzOP2y ago
yes, it's not supposed to have a T the password should fail if it has a T
baykkz
baykkzOP2y ago
but it doesn't
baykkz
baykkzOP2y ago
and this part is preceded by a Not detected: haha
Angius
Angius2y ago
Wait, I got myself confused lol
baykkz
baykkzOP2y ago
so it should print Not detected: T absence yeah the code is a bit messy, sorry
Angius
Angius2y ago
Ah I know
baykkz
baykkzOP2y ago
its so I can see what went wrong
Angius
Angius2y ago
It's a set of if/else if statements Only one of them will execute T is uppercase
baykkz
baykkzOP2y ago
OMG
Angius
Angius2y ago
So the code hits if (char.IsUpper(letter)) HasUpper = true; and peaces out
baykkz
baykkzOP2y ago
THAT'S IT IT'S WORKING thank you so much
Angius
Angius2y ago
Nice
baykkz
baykkzOP2y ago
nice eye lol, would have taken me a week to get this ok, I can move on now Thanks a lot! @Angius Have a great day
Angius
Angius2y ago
Likewise 👌
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server