C
C#15mo ago
Fivesssss

❔ Issue with Creating a looping system for and entire program

So I made this block of code in order to loop my entire program and my issue is if theres a valid input by the user it fails to go through the first if else check and outputs the invalid input Text. I wonder what im doing wrong here.
22 Replies
Angius
Angius15mo ago
Let's see how the boolean logic for input Y works out
Pobiega
Pobiega15mo ago
Look at your if. Read it out loud.
Angius
Angius15mo ago
a || b && c || d with a being false and everything else being true false || true && true || true true && true true
Pobiega
Pobiega15mo ago
I would also highly recommend sticking the asking for yes/no in a reusable method. Will make your main code more readable
Fivesssss
FivesssssOP15mo ago
can you elaborate on that please?
Angius
Angius15mo ago
Something like
private bool ShouldContinue()
{
Console.WriteLine(...);
var input = Console.ReadLine();
return input is "Y" or "y";
}
private bool ShouldContinue()
{
Console.WriteLine(...);
var input = Console.ReadLine();
return input is "Y" or "y";
}
if (ShouldContinue())
{
// continue
}
else
{
// shutdown
}
if (ShouldContinue())
{
// continue
}
else
{
// shutdown
}
Fivesssss
FivesssssOP15mo ago
so I should create a method for looping the program instead of having it setup in main
Pobiega
Pobiega15mo ago
Well the loop itself still goes on Main
Angius
Angius15mo ago
No, you should create a method that asks the user if it should continue or not
Pobiega
Pobiega15mo ago
Here is one I use
public static bool YesOrNo(string prompt)
{
while (true)
{
Console.Write(prompt);
var response = Console.ReadLine();

switch (response?.ToLowerInvariant())
{
case "y" or "yes" or "true":
return true;
case "n" or "no" or "false":
return false;
default:
Console.WriteLine("Invalid format. Try again.");
break;
}
}
}
public static bool YesOrNo(string prompt)
{
while (true)
{
Console.Write(prompt);
var response = Console.ReadLine();

switch (response?.ToLowerInvariant())
{
case "y" or "yes" or "true":
return true;
case "n" or "no" or "false":
return false;
default:
Console.WriteLine("Invalid format. Try again.");
break;
}
}
}
Angius
Angius15mo ago
Although I could say that can wait Learning how boolean logic works is a much more fundamental thing
Pobiega
Pobiega15mo ago
true because lets read it out
Angius
Angius15mo ago
Introducing methods, switches, pattern matching, all that jazz before booleans is backwards
Pobiega
Pobiega15mo ago
"if userResponse is not lower case y OR userResponse is not upper case Y..."
Fivesssss
FivesssssOP15mo ago
I did a course where boolean logic was taught so I do understand it to a certain extent but I completly forgot about it till now
Angius
Angius15mo ago
Well, try to rewrite your statement then In a way that makes sense, boolean logic-wise Use a truth table if need be
Pobiega
Pobiega15mo ago
yeah remember order of operations, and that each condition is evaluated individually. ie, the second comparison doesnt know what the first one checked so (a != 'Y' || a != 'y') will always be true. There are no values for a where its ever false
Angius
Angius15mo ago
spoonfeed
Fivesssss
FivesssssOP15mo ago
ah ok thanks for the help guys
Denis
Denis15mo ago
$close
MODiX
MODiX15mo ago
Use the /close command to mark a forum thread as answered
Accord
Accord15mo 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