✅ 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
Angius13mo 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`?
}
泣きたいですか
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
Angius13mo ago
Well, you can turn it into a method so it's reusable
泣きたいですか
how so?
Aokiri 🐸
Aokiri 🐸13mo ago
You can, also, clean that code using verbatim strings. Like @$"All the text with any lines and etc"
Angius
Angius13mo 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();
泣きたいですか
I do not know what those are, my professor does not recommend us to use material that we have not yet learned
Aokiri 🐸
Aokiri 🐸13mo ago
Oh, ok.
Angius
Angius13mo ago
Raw strings would be better, but sure
泣きたいですか
what does static and the end lines do?
Angius
Angius13mo 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"?
泣きたいですか
the last 3 lines of code
Angius
Angius13mo ago
Three variables That get the value from the Ask() method
泣きたいですか
I see
Angius
Angius13mo 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.");
}
泣きたいですか
I see should I give you an example of the first flavor's calculation?
Angius
Angius13mo ago
Sure
Angius
Angius13mo 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
泣きたいですか
this is what happens when I run the code
No description
泣きたいですか
as you can see, I use "false" instead of "n" I see, well, that would take quite a long time haha
Angius
Angius13mo ago
Which is why I said to flatten the code Instead of having an exponential amount of branches
Jimmacle
Jimmacle13mo ago
i refuse to believe your professor expects you to write code that KEKW
泣きたいですか
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
Angius13mo ago
You absolutely can You have all the knowledge needed
泣きたいですか
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
Angius13mo 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
Jimmacle13mo ago
yeah, your code has a serious amount of redundant branches nesting conditions that don't depend on each other
Angius
Angius13mo ago
If you're averse to using methods, then sure
泣きたいですか
the problem with this is that we still have not learned about the "ask" thing
Angius
Angius13mo ago
So, about methods?
泣きたいですか
all of my other classmate's code is also pretty big, well, mine is the smallest one so far
Jimmacle
Jimmacle13mo ago
that's...
泣きたいですか
there is this one guy that has 1200 lines of code just for the vanilla flavor
Jimmacle
Jimmacle13mo 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
泣きたいですか
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
Jimmacle13mo ago
exactly
Angius
Angius13mo ago
Yep Don't nest the questions
Jimmacle
Jimmacle13mo ago
that should be like a couple hundred lines at most
泣きたいですか
that is what I have thought as well but it turned out pretty big
Jimmacle
Jimmacle13mo 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
泣きたいですか
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
Jimmacle13mo ago
sounds like your professor might not be that great, which honestly isn't uncommon
泣きたいですか
so the things that you guys are showing me is pretty new 95% of the people hate him
Angius
Angius13mo ago
Is it new? Not-nested code is a step below what you wrote tbh
泣きたいですか
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
Jimmacle13mo 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
泣きたいですか
it depends on how the learning material is presented
Angius
Angius13mo ago
Less code means it's easier to find errors
Jimmacle
Jimmacle13mo ago
if you're doing the same thing in both branches of a condition, it shouldn't be inside the condition at all
泣きたいですか
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
Angius13mo ago
Here's a more direct example should you need it
No description
Angius
Angius13mo ago
Not complete, not as complex as what you need, but should give you a direction if necessary
泣きたいですか
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
Accord13mo 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