❔ tutoring
WriteLine("Enter number of exterior murals scheduled >>");
int exteriorMural=Convert.ToInt32(ReadLine());
if(monthNumber==1monthNumber==2monthNumber==12){
WriteLine("Due to uncertain weather conditions, exterior murals cannot be painted at this time");
exteriorMural=0;}
const int EXTCOST=750;
int finalExteriorCost=699;
if(monthNumber==4monthNumber==5monthNumber==9||monthNumber==10){
finalExteriorCost=699;
}
I got the first if statement to work how I wanted it to but for some reason the next if statement isn't working at all.
35 Replies
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
If your code is too long, post it to: https://paste.mod.gg/$codegif
alright, so the second if statement isnt working, you say?
Yeah
it looks to me as if it does nothing
int finalExteriorCost=699;
<--the starting value is 699
if its month 4,5, 9 or 10, you set it to.. 699
which it already wasThat's what I thought too, but my instructor said it had to have a value assigned.
it has.
int finalExteriorCost=699;
assigns a value.That's what I said. lol
What would you recommend I do to fix it?
fix what?
The second if statement.
you could just straight up remove that if statement, as the code is right now
it does nothing
its just setting the same value as is already there
Okay, thank you.
There are a few other things I'd consider about this program.
For example, if you cant paint external murals during month 12 1 2, why even ask?
so if the month is 12 1 2, don't ask for external mural count
also, this is how we usually format C# 🙂
you should also probably have the number of external murals factor into the total cost
like,
finalExteriorCost += exteriorMural * EXTCOST;
or somethingI didn't share the whole code, just the snippet that I needed help with. I didn't wanna eat up too much of your time so I only shared what I thought was relevant. I was trying to get the code to not even check for external murals if the months 1, 2, or 12 were entered but I couldn't get it to work that way so my instructor said it was fine the way it was.
If the month is anything other than what was typed in the code, then it's supposed to function as normal. I was trying to create parameters as necessary for the code I was trying to do and from my understanding an if statement does that.
As a side note, I've never touched computer programming before this semester so I'm learning C# from scratch and I'm still very new, but I understand it if it's explained to me in a more logical and black and white way since my brain thinks about all of this in a mathematical way. I greatly appreciate any and all time that someone here is willing to take out of their day to help me, especially since I know they have their own things going on.
Absolutely nothing wrong with being new, and you have the right attitude for learning (and being respectful to the helpers)! Feel free to ask more questions if you have, the beauty of discord is that people can pop in and write when its convenient for them
Thank you so much! I'll definitely ask if I have anymore questions!
Beautiful code by the way. I've been looking at it to understand how it was done and it's very well organized. I'll definitely be trying to format it that way from now on.
if you are using Visual Studio or Rider, you have automatic formatting already
What's the difference between Parse and Convert, exactly? Because from my understanding, it does the same thing.
I'm using Cengage right now, but I plan to use Visual Studio when I code something once I learn it all.
they do ALMOST the same thing, its just that
Convert.ToInt32
has more edge casesEdge cases? What do you mean?
okay, so...
"100"
that should be 100, yeah? no questions there
Yeah
both parse and convert handles that as expected
but what about...
null
? or ""
what should they be?
Convert thinks it should be 0. Parse thinks that it cant be turned into a number. I tend to agree with Parse.Gotcha. So Parse is more of a general while Convert takes it literally.
there is an even better version called
TryParse
, that lets you handle the error cases better
its more advanced thou, but very good to know
$tryparseThe TryParse pattern is considered best practice of parsing data from a string:
- a TryParse method returns
true
or false
to inform you if it succeeded or not, so you can use it directly in a condition,
- since C# 7 you can declare a variable that will be used as an out
argument inline in an argument list,
- it forces you to check if the out
argument contains valid data afterwards,
Avoid: Convert.ToInt32 — it's a bad choice for parsing an int
. It exists only for backwards compatibility reasons and should be considered last resort. (Note: Convert does contain useful conversion methods: To/FromBase64String
, To/FromHexString
, ToString(X value, int toBase)
, ToX(string? value, int fromBase)
)
Avoid: int.Parse — you have to use a try
/catch
statement to handle invalid input, which is a less clean solution.
Use int.TryParse https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-5.0#System_Int32_TryParse_System_String_System_Int32__ Int32.TryParse Method (System)
Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.
My instructor said we'll be getting to TryParse later when I shared what we went over here.
Great
It's very good to know though! Thank you so much for explaining this to me! Me and a classmate didn't know the difference at all so we were confused. lol
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.