C
C#2y ago
Cry

✅ need help converting "y" or "n" response to boolean "true" or "false" in .NET 7

Hello, I have written about 3500+ lines of code, I need to make it so the user input of "y" or "n" is converted to boolean "true" or "false", would it be possible to make these conversions before the entire code? my code works perfectly, though I have to use "true" or "false" instead of "y" or "n", so it would be preferable if I had it converted or parsed before the important lines of code. as mentioned, I am using .NET 7 and I am rather new to programming in general, C# is my first programming language and I have been coding in C# for about 1 month now.
55 Replies
Angius
Angius2y ago
You can do that with an if statement, for example
var input = Console.ReadLine();
var someBool;
if (input == "y")
{
someBool = true;
}
else if (input == "n")
{
someBool = false;
}
else
{
// what to do when the input is neither `y` nor `n`?
}
var input = Console.ReadLine();
var someBool;
if (input == "y")
{
someBool = true;
}
else if (input == "n")
{
someBool = false;
}
else
{
// what to do when the input is neither `y` nor `n`?
}
Cry
CryOP2y ago
I see, I will try this out, for a more detailed explanation about my case is: I am trying to code a program which allows me to select a choice between 8 ice cream flavors, there are also extra toppings and all that. I need to make it so when it asks me if I would like extra whip cream, I can respond with a simple "y" or "n" answer so if it would be possible to put this If statement before all the other lines of code.
No description
Angius
Angius2y ago
Well, you can turn it into a method so it's reusable
Cry
CryOP2y ago
how so?
Aokiri 🐸
Aokiri 🐸2y ago
You can, also, clean that code using verbatim strings. Like @$"All the text with any lines and etc"
Angius
Angius2y ago
For example,
static bool Ask()
{
var input = Console.ReadLine();
if (input == "y")
{
return true;
}
else if (input == "n")
{
return false;
}
else
{
// what to return when the input is neither `y` nor `n`?
}
}

var a = Ask();
var b = Ask();
var c = Ask();
static bool Ask()
{
var input = Console.ReadLine();
if (input == "y")
{
return true;
}
else if (input == "n")
{
return false;
}
else
{
// what to return when the input is neither `y` nor `n`?
}
}

var a = Ask();
var b = Ask();
var c = Ask();
Cry
CryOP2y ago
I do not know what those are, my professor does not recommend us to use material that we have not yet learned
Aokiri 🐸
Aokiri 🐸2y ago
Oh, ok.
Angius
Angius2y ago
Raw strings would be better, but sure
Cry
CryOP2y ago
what does static and the end lines do?
Angius
Angius2y ago
static just means the method doesn't need an instance of an object Since you're writing top-level code you might not even need that What do you mean by "end lines"?
Cry
CryOP2y ago
the last 3 lines of code
Angius
Angius2y ago
Three variables That get the value from the Ask() method
Cry
CryOP2y ago
I see
Angius
Angius2y ago
Console.WriteLine("Do you want whipped cream?");
bool shouldAddWhippedCream = Ask();
if (shouldAddWhippedCream)
{
Console.WriteLine("Yes, please");
}
else
{
Console.WriteLine("No, thank you.");
}
Console.WriteLine("Do you want whipped cream?");
bool shouldAddWhippedCream = Ask();
if (shouldAddWhippedCream)
{
Console.WriteLine("Yes, please");
}
else
{
Console.WriteLine("No, thank you.");
}
Cry
CryOP2y ago
I see should I give you an example of the first flavor's calculation?
Angius
Angius2y ago
Sure
Cry
CryOP2y ago
Angius
Angius2y ago
Yeah, you would replace your bool.Parse(Console.ReadLine()) with that Ask() method call Also, it would be better to flatten your code instead of nesting it infinitely Ask for sauce once, save the result in a variable Ask for whipped cream once, save it in a variable Then, sum it all up at the end
Cry
CryOP2y ago
this is what happens when I run the code
No description
Cry
CryOP2y ago
as you can see, I use "false" instead of "n" I see, well, that would take quite a long time haha
Angius
Angius2y ago
Which is why I said to flatten the code Instead of having an exponential amount of branches
Jimmacle
Jimmacle2y ago
i refuse to believe your professor expects you to write code that KEKW
Cry
CryOP2y ago
that would be best, though at my level, I can not do such a thing, since I have recently learned about how switches and swaps work
Angius
Angius2y ago
You absolutely can You have all the knowledge needed
Cry
CryOP2y ago
it is quite hard to get a rough estimate of my C# knowledge, since I only study what I am taught in school would it be okay to just copy your code and paste it multiple times in my code's branches?
Angius
Angius2y ago
Pseudocode:
decimal price = 0.0m;

if (ask('want sprinkles?'))
price += sprinkles_price;

if (ask('want sauce?'))
price += sauce_price;

if (ask('want cream?'))
price += cream_price;
decimal price = 0.0m;

if (ask('want sprinkles?'))
price += sprinkles_price;

if (ask('want sauce?'))
price += sauce_price;

if (ask('want cream?'))
price += cream_price;
Jimmacle
Jimmacle2y ago
yeah, your code has a serious amount of redundant branches nesting conditions that don't depend on each other
Angius
Angius2y ago
If you're averse to using methods, then sure
Cry
CryOP2y ago
the problem with this is that we still have not learned about the "ask" thing
Angius
Angius2y ago
So, about methods?
Cry
CryOP2y ago
all of my other classmate's code is also pretty big, well, mine is the smallest one so far
Jimmacle
Jimmacle2y ago
that's...
Cry
CryOP2y ago
there is this one guy that has 1200 lines of code just for the vanilla flavor
Jimmacle
Jimmacle2y ago
i mean, that's good that you're the best, but there's a lot more that can be done to shorten it 3500 lines for a menu with 6 choices is insane
Cry
CryOP2y ago
yeah, that is true, though my professor insists on writing it with the resources that we are taught in school well 8 choices then 1 questions another question another question another question = sum of the bill
Jimmacle
Jimmacle2y ago
exactly
Angius
Angius2y ago
Yep Don't nest the questions
Jimmacle
Jimmacle2y ago
that should be like a couple hundred lines at most
Cry
CryOP2y ago
that is what I have thought as well but it turned out pretty big
Jimmacle
Jimmacle2y ago
even without methods, look at what zzzzz is suggesting about not nesting things that don't need to be nested generally if you find yourself copying and pasting the same thing a lot something is wrong
Cry
CryOP2y ago
yeah, that is true though even my professor has told me that we are 2 weeks behind material, since he was sick for a whole week + other things happened
Jimmacle
Jimmacle2y ago
sounds like your professor might not be that great, which honestly isn't uncommon
Cry
CryOP2y ago
so the things that you guys are showing me is pretty new 95% of the people hate him
Angius
Angius2y ago
Is it new? Not-nested code is a step below what you wrote tbh
Cry
CryOP2y ago
It is currently 11 pm at my place, I have been writing this code for over 4 hours, it works but not how I intended it to work
Jimmacle
Jimmacle2y ago
anyway, start by looking at all your conditions and only put conditions inside other conditions if the code is actually different based on the outer condition
Cry
CryOP2y ago
it depends on how the learning material is presented
Angius
Angius2y ago
Less code means it's easier to find errors
Jimmacle
Jimmacle2y ago
if you're doing the same thing in both branches of a condition, it shouldn't be inside the condition at all
Cry
CryOP2y ago
I see well, this task was optional, only a few of my classmates have tried it, I am the first one to finish it fully it is the most difficult task we have been given so far I thank you guys for helping me out, I shall try the methods that you have suggested to me.
Angius
Angius2y ago
Here's a more direct example should you need it
No description
Angius
Angius2y ago
Not complete, not as complex as what you need, but should give you a direction if necessary
Cry
CryOP2y ago
oh I see, I believe that this would help me a lot, I will try to rewrite my code using this method after submitting my 1st attempt.
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.

Did you find this page helpful?