❔ Need Help finishing up an assignment, getting some errors as well
Total starter to C#, made some decent progress on an assignment on my own but need some help finishing it up.
144 Replies
$ask
How to get the best help
Make a post in #help or one of the topic channels under Development.
Avoid asking Can anybody help Has anyone used Why doesn't my code work?
C# is a big area! No one knows they can help unless you tell them about the small area you're trying to work in.
Explain what you are doing, and potentially why for as much context as possible. Avoid screenshots where possible, share code directly in Discord. Type
$code
into chat to learn how to post code.
See https://www.nohello.net and https://dontasktoask.com if you want common help chat room etiquette.Also we do not write homeworks for you, only help with questions that doesn't involve cheating
yeah you need to ask specific questions about what you're stuck on
main thing would be these errors i suppose
It seems like
GetItemQuantity
method is nested within GetItemNum
, which makes it localwell, one of your files has a lot of things that look like they should be in a class but aren't
Local members can't have access modifiers
alright, item by item then
public void enterSales, "the modifier public is not valid for this item"
right
so in C# you can't have "free functions"
methods must be defined inside a class
Angius
It seems like
GetItemQuantity
method is nested within GetItemNum
, which makes it localQuoted by
<@85903769203642368> from #Need Help finishing up an assignment, getting some errors as well (click here)
React with ❌ to remove this embed.
everything in your
CalculateCommission
file is not in a class definition, which is probably generating 99% of your errorsi got the GetItemQuality nesting fixed
Always keep $structure in mind
Methods go into classes
Besides the one and only
Program.cs
file that can contain top-level code"a namespace cannot directly contain members such as fields, methods or statements" on line 6
yes, it's telling you the same thing we are
okay, it's part of the starter code of the assignment, so where should i move it to
like i apologize if im asking basic question, i'm literally just starting with C# this month
it's telling you
a namespace cannot directly contain members such as fields, methods or statements
because those things have to be inside a class, so move it inside a class
i see why you're confused, the assignment doesn't seem to tell you thatno, and it doesnt at all tell me whats supposed to go into program.cs either XD
'the namespace <global namespace> already contains a definition for StartApp'
just needed to put a namespace at the top there i guess
okay, last error but its listed twice
"CalculateCommission is a namespace but it used like a type"
public void Start() { DisplayAssignmentHeading(); CalculateCommission calculateCommission = new CalculateCommission(); calculateCommission.EnterSales(); }
well, did you make a namespace or a class named CalculateCommission?
that code is expecting a class with that name
both
namespace CalculateCommission;in CalculateCommission.cs
public class CalculateCommission { private decimal totalSales = 0;
all your files should probably use the same namespace for simplicity's sake
generally namespaces follow the folder structure of the project
okay using namespace CalculateComission on StartApp worked, thank you
oh i guess program.cs needs a main method?
you need a main method somewhere in your project, it doesn't have to be in that specific file
$mains
The possible signatures for
Main
are public
is not required (can be any accessibility).
Top-level statements are compiled into a Main
method and will use an appropriate signature depending on the body.
https://docs.microsoft.com/en-US/dotnet/csharp/fundamentals/program-structure/main-command-lineMain() and command-line arguments - C#
Learn about Main() and command-line arguments. The 'Main' method is the entry point of an executable program.
that's how your computer knows where to start running your program
"the modifier 'public' is not valid for this item" for public static void main
using CalculateCommission; public static void Main() { StartApp startApp = new StartApp(); startApp.Start(); }
remember what we talked about re: where you're allowed to define methods
okay, passing errors
for this
private void CalculateItemSale(int itemQuantity, decimal itemCost) { // code to calculate item sales // code to update totalSales running total while (itemQuantity != -1) {Can i just use nested if statements for each quantity 1-5 with an equation for each being itemQuantity * itemPrice, or can i set up item price in each iteration and do the math at the end?
} } }
first, fix the indentation so we can read it without crying code using $code
Posting Code Snippets
To post a code snippet type the following:
```cs
// code here
```
Notes:
- Get an example by typing
$codegif
in the chat.
- Change the language by replacing cs
with the language of your choice (for example sql
or cpp
).
- If your code is too long, you can post it to https://paste.mod.gg/ and share the link.$codegif
'''cs
private void CalculateItemSale(int itemQuantity, decimal itemCost)
{
// code to calculate item sales
// code to update totalSales running total
while (itemQuantity != -1)
{
} } } ''' im crying
} } } ''' im crying
` is the key to the left of 1 on the top row
with ~
whenever you start a new { } block you should indent what's inside it
hmm now that i think, i should be able to still use itemNum in this, right?
well, what is
CalculateItemSale
supposed to do?number of items orders times the price of that particular item, but each of the 5 items has a different price
but its confusing because part of the first method also seems to be for that
here's the full CalculateCommissions.cs file
sadness i cannot
it looks like
EnterSales()
already has some basic checks to handle the -1s
so your code only has to worry about 1 to 5correct, that's why i have != -1
1-5 are the only other valid inputs it accepts so i should just be able to do it like that, yeah?
yeah
would i need to declare itemNum in the private void to use it from the GetItemNum function? its giving me "the name itemNum does not exist in the current context" when i try to use it
where is
itemNum
currently defined?or
both definied in public void EnterSales
$scopes
thing a
is available in scope A
and scope B
thing b
is available only in scope B
This will let you decide where to put what
only itemNum is giving me an error
you shouldn't be nesting methods in other methods
its not, just for ease of showing
it doesn't make reading it easy at all beacuse it changes how your code works
beginning and end of the file, too big to send the whole thing
we can't help based on code that doesn't actually exist that way
i dont know how you want me to show you the entire file when i cant
all of it is nested within public class CalculateComission
whew
so, i want you to look at that big chain of if statements
and tell me why it's there
the method doesn't do what the comment says it should do
because the entirety of my instruction for this unit is using if-else statements and literally nothing has actually been taught to me by the professor
you know that you can compare numbers with
>
and <
?yes
this is our 'learning material' for this unit, 4 slides of code
well, it technically has everything you need in there
might not necessarily be the best way to teach it if you're just having slides dumped on you
exactly
for GetItemNum() for example, what happens if i type a 7
it should give you "Invalid input"
why? it's a number
so
int.TryParse
will return true
if you want specific numbers to be invalid you have to write the code to check for them
which you sort of have, but it doesn't actually do anything and your method effectively just returns any number the user typesso i need to add one in at the end for an else if itemNum >= 6?
that would be a good thing to have
you only want to return a number that is either -1 or between 1 and 5, maybe that phrasing can help you optimize the logic in that method
(you can technically check all of that with a single if statement)
It will need checking in the while condition won't it, because they're supposed to keep looping until they get valid input?
yes, but the existing checks are already in the loop
Ah I thought you were recommending to get rid of the current loop body
nah, just the chain of if statements
this whole part
is effectively simply
Yeah, so I assumed scrap that, move the return outside the while loop, change while condition
yeah the loop could be restructured but it's fine for now
so what should i be doing?
figure out what logic you need so that the method will either return -1 or 1-5, or ask the user for a different number
okay
thats what im asking for help with
like should i just change each one to >= 1, >=2, etc?
not quite
so you want to check if a number equals -1 or is 1 or higher and 5 or lower
do you think you can turn that into C# logic?
wouldnt it just be this
but 5/4/3/2/1
you would use comparison operators like that, yes
well, no
if you check if a number is both >= 1 and <= 5, then you know it can be 1, 2, 3, 4, or 5
you don't have to check every single number
well
-1 is valid but 0 isn't
right, that's a different case that you have to handle
Jimmacle
so you want to check if a number equals -1 or is 1 or higher and 5 or lower
Quoted by
<@901546879530172498> from #Need Help finishing up an assignment, getting some errors as well (click here)
React with ❌ to remove this embed.
so would it be better to just do an if for each case then?
pretty much
except for the higher and lower checks you have to make sure they're both true
and then just make another for like, >=6, =0, and <=-2?
for example, 7 is bigger than 1 but it's not smaller than 5 so it should be invalid
yeah
just do three more ifs with error statements
you don't need to check for that, because you're already checking for valid numbers
so if it's not a valid number then it's invalid
for example, 0 isn't invalid because it's 0, it's invalid because it's not -1, 1, 2, 3, 4, or 5
got it
so i just do if's for the ones i want, and else accounts for all other cases
right
so for this
i just replace return itemNum with else
{
error message
}
yep
you don't want to return there because at that point you know the number isn't a valid one
yeah
if you wanted to make the method a lot shorter you could turn the whole if chain into
now i have the same two errors a bunch
"a local or parameter named 'itemCost' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter"
and
"the name 'itemNum' does not exist in the current context"
for this one look at what is being passed into the method
you should expect to already have the item quantity and the item cost, so do you need any more information to calculate the total price?
yes, which item it is, because the items have different prices
but look
you are already getting
itemCost
from whatever is calling this method
it's being passed into the method, so you don't have to look it up by item numberwell the price of each item isn't stored anywhere else
this method is the only place the variable itemCost is even mentioned
it must be stored somewhere else, because whatever calls this method has to pass in a cost
would i put it in the GetItemNum method?
i pasted the entire code starting here
https://discord.com/channels/143867839282020352/1158482223801053244/1158500197500997642
Reinhardt
Quoted by
<@129328138382475264> from #Need Help finishing up an assignment, getting some errors as well (click here)
React with ❌ to remove this embed.
what i'm trying to point out is that it's not the "job" of this method to look up the price for an item
whatever code uses this method needs to provide it
// code to calculate item sales
// code to update totalSales running total
it needs to do itemQuantity * itemCost, but to get the right itemCost it needs the proper itemNum to assign it
no it doesn't
look at this part
private void CalculateItemSale(int itemQuantity, decimal itemCost)
what data is being provided to the method?the number of items, and the cost of the item
right
so why do you need the item number to find the cost if you already have the cost of the item?
because there are 5 different possible costs
but you don't care here
whatever cost you need is provided in the
itemCost
variable alreadywhich isnt stored anywhere else but this method
don't worry about that for now, this is about solving one piece of the problem at the time
this particular part's job is to take a quantity and a cost, and update the running total right?
yes
so that's all you have to do in this method
you'll write more code somewhere else that handles turning an item number into a cost, then that code will pass the correct cost into this method
now just gives me one of the 'a local or parameter' errors
yes, because you are trying to declare
itemCost
but it's already declared in the method argumentson the decimal itemCost
that's part of the starter code
er, no
okay
so then am i putting the individual prices for the numbers in
that makes sense to me
and would i do it the way i had it before and just invoke CalculateItemSale
right
Doesn't that total sales calculation need correcting first?
?
yes but one step at a time
he's referring to the fact that
totalSales = itemQuantity * itemCost;
will ignore whatever the value of totalSales
was previouslyso i can just do
and so on? inside of
yeah basically
you figure out the item cost there, then pass it in when you call CalculateItemSale
nope
'the name itemCost does not exist in the current context
you can define as many variables as you need to get the job done
keep in mind the
itemCost
inside CalculateItemSale
only exists inside that method
you may have a different variable named itemCost
defined in your other method where you store the cost before passing it inokay
i dont have any errors but it fails to build
it looks like your program is still running in the background somewhere
does it work though? might just be my computer
it is just your computer
you have to stop the program from running before you can rebuild it
it doesnt seem to be running on my end?
that error only happens if it is
if in doubt restart pc
my thing says 'Ready' and when i hover over it it says 'No background tasks running'
the program functions as intended though, you get a total price and everything printing out?
if it works, i dont care if it works for me XD
idk, i didn't try it
you should test and make sure it works
i closed visual studio and reoppened and got the same error
restarted computer, going to run again
okay, it just goes back and forth on item number and quantity
don't want to be annoying but @Jimmacle almost there if you're still free
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.