C
C#2y ago
xllint

❔ 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
Pobiega
Pobiega2y ago
$code
MODiX
MODiX2y ago
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/
xllint
xllintOP2y ago
$codegif
xllint
xllintOP2y ago
WriteLine("Enter number of exterior murals scheduled >>");
int exteriorMural=Convert.ToInt32(ReadLine());
if(monthNumber==1||monthNumber==2||monthNumber==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==4||monthNumber==5||monthNumber==9||monthNumber==10){
finalExteriorCost=699;
}
WriteLine("Enter number of exterior murals scheduled >>");
int exteriorMural=Convert.ToInt32(ReadLine());
if(monthNumber==1||monthNumber==2||monthNumber==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==4||monthNumber==5||monthNumber==9||monthNumber==10){
finalExteriorCost=699;
}
Pobiega
Pobiega2y ago
alright, so the second if statement isnt working, you say?
xllint
xllintOP2y ago
Yeah
Pobiega
Pobiega2y ago
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 was
xllint
xllintOP2y ago
That's what I thought too, but my instructor said it had to have a value assigned.
Pobiega
Pobiega2y ago
it has. int finalExteriorCost=699; assigns a value.
xllint
xllintOP2y ago
That's what I said. lol What would you recommend I do to fix it?
Pobiega
Pobiega2y ago
fix what?
xllint
xllintOP2y ago
The second if statement.
Pobiega
Pobiega2y ago
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
xllint
xllintOP2y ago
Okay, thank you.
Pobiega
Pobiega2y ago
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
int exteriorMural;
if (monthNumber == 1 || monthNumber == 2 || monthNumber == 12)
{
Console.WriteLine("Due to uncertain weather conditions, exterior murals cannot be painted at this time");
exteriorMural = 0;
}
else
{
Console.WriteLine("Enter number of exterior murals scheduled >>");
exteriorMural = int.Parse(Console.ReadLine());
}

const int EXTCOST = 750;
int finalExteriorCost = 699;

if (monthNumber == 4 || monthNumber == 5 || monthNumber == 9 || monthNumber == 10)
{
finalExteriorCost = 699;
}
// what if month is 6 7 8 11?
int exteriorMural;
if (monthNumber == 1 || monthNumber == 2 || monthNumber == 12)
{
Console.WriteLine("Due to uncertain weather conditions, exterior murals cannot be painted at this time");
exteriorMural = 0;
}
else
{
Console.WriteLine("Enter number of exterior murals scheduled >>");
exteriorMural = int.Parse(Console.ReadLine());
}

const int EXTCOST = 750;
int finalExteriorCost = 699;

if (monthNumber == 4 || monthNumber == 5 || monthNumber == 9 || monthNumber == 10)
{
finalExteriorCost = 699;
}
// what if month is 6 7 8 11?
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 something
xllint
xllintOP2y ago
I 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.
Pobiega
Pobiega2y ago
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
xllint
xllintOP2y ago
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.
Pobiega
Pobiega2y ago
if you are using Visual Studio or Rider, you have automatic formatting already
xllint
xllintOP2y ago
What's the difference between Parse and Convert, exactly? Because from my understanding, it does the same thing.
Pobiega
Pobiega2y ago
xllint
xllintOP2y ago
I'm using Cengage right now, but I plan to use Visual Studio when I code something once I learn it all.
Pobiega
Pobiega2y ago
they do ALMOST the same thing, its just that Convert.ToInt32 has more edge cases
xllint
xllintOP2y ago
Edge cases? What do you mean?
Pobiega
Pobiega2y ago
okay, so... "100" that should be 100, yeah? no questions there
xllint
xllintOP2y ago
Yeah
Pobiega
Pobiega2y ago
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.
xllint
xllintOP2y ago
Gotcha. So Parse is more of a general while Convert takes it literally.
Pobiega
Pobiega2y ago
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 $tryparse
MODiX
MODiX2y ago
The 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.
return Convert.ToInt32(null); //returns 0. null should not be considered as 0 ever
return Convert.ToInt32("asdf"); //throws FormatException
return Convert.ToInt32(null); //returns 0. null should not be considered as 0 ever
return Convert.ToInt32("asdf"); //throws FormatException
(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.
var number = int.Parse("abc"); //throws FormatException
var number = int.Parse(""); //throws FormatException
var number = int.Parse("abc"); //throws FormatException
var number = int.Parse(""); //throws FormatException
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__
if (int.TryParse(someInput, out var result))
{
Console.WriteLine($"Thanks for giving me the following number: {result}!");
}
else
{
Console.WriteLine("You didn't give me a valid number :c");
}
if (int.TryParse(someInput, out var result))
{
Console.WriteLine($"Thanks for giving me the following number: {result}!");
}
else
{
Console.WriteLine("You didn't give me a valid number :c");
}
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.
xllint
xllintOP2y ago
My instructor said we'll be getting to TryParse later when I shared what we went over here.
Pobiega
Pobiega2y ago
Great
xllint
xllintOP2y ago
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
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.
Want results from more Discord servers?
Add your server