C
C#10mo 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
Angius10mo ago
Let's see how the boolean logic for input Y works out
Pobiega
Pobiega10mo ago
Look at your if. Read it out loud.
Angius
Angius10mo ago
a || b && c || d with a being false and everything else being true false || true && true || true true && true true
Pobiega
Pobiega10mo ago
I would also highly recommend sticking the asking for yes/no in a reusable method. Will make your main code more readable
Fivesssss
Fivesssss10mo ago
can you elaborate on that please?
Angius
Angius10mo 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
Fivesssss10mo ago
so I should create a method for looping the program instead of having it setup in main
Pobiega
Pobiega10mo ago
Well the loop itself still goes on Main
Angius
Angius10mo ago
No, you should create a method that asks the user if it should continue or not
Pobiega
Pobiega10mo 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
Angius10mo ago
Although I could say that can wait Learning how boolean logic works is a much more fundamental thing
Pobiega
Pobiega10mo ago
true because lets read it out
Angius
Angius10mo ago
Introducing methods, switches, pattern matching, all that jazz before booleans is backwards
Pobiega
Pobiega10mo ago
"if userResponse is not lower case y OR userResponse is not upper case Y..."
Fivesssss
Fivesssss10mo 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
Angius10mo 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
Pobiega10mo 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
Angius10mo ago
spoonfeed
Fivesssss
Fivesssss10mo ago
ah ok thanks for the help guys
Denis
Denis10mo ago
$close
MODiX
MODiX10mo ago
Use the /close command to mark a forum thread as answered
Accord
Accord10mo 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.