✅ 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
You can do that with an
if
statement, for example
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.
Well, you can turn it into a method so it's reusable
how so?
You can, also, clean that code using verbatim strings.
Like @$"All the text with any lines and etc"
For example,
I do not know what those are, my professor does not recommend us to use material that we have not yet learned
Oh, ok.
Raw strings would be better, but sure
what does static and the end lines do?
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
Three variables
That get the value from the
Ask()
methodI see
I see
should I give you an example of the first flavor's calculation?
Sure
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 endthis is what happens when I run the code
as you can see, I use "false" instead of "n"
I see, well, that would take quite a long time haha
Which is why I said to flatten the code
Instead of having an exponential amount of branches
i refuse to believe your professor expects you to write code that
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
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?
Pseudocode:
yeah, your code has a serious amount of redundant branches
nesting conditions that don't depend on each other
If you're averse to using methods, then sure
the problem with this is that we still have not learned about the "ask" thing
So, about methods?
all of my other classmate's code is also pretty big, well, mine is the smallest one so far
that's...
there is this one guy that has 1200 lines of code just for the vanilla flavor
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
exactly
Yep
Don't nest the questions
that should be like a couple hundred lines at most
that is what I have thought as well
but it turned out pretty big
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
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
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
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
Less code means it's easier to find errors
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.
Here's a more direct example should you need it
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.
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.