C
C#9mo ago
Reinhardt

❔ 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
Buddy
Buddy9mo ago
$ask
MODiX
MODiX9mo ago
How to get the best help catpog Make a post in #help or one of the topic channels under Development. Avoid askingcatthinking Can anybody help catthinking Has anyone used catthinking 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.
Buddy
Buddy9mo ago
Also we do not write homeworks for you, only help with questions that doesn't involve cheating
Jimmacle
Jimmacle9mo ago
yeah you need to ask specific questions about what you're stuck on
Reinhardt
Reinhardt9mo ago
main thing would be these errors i suppose
Reinhardt
Reinhardt9mo ago
No description
Angius
Angius9mo ago
It seems like GetItemQuantity method is nested within GetItemNum, which makes it local
Jimmacle
Jimmacle9mo ago
well, one of your files has a lot of things that look like they should be in a class but aren't
Angius
Angius9mo ago
Local members can't have access modifiers
Reinhardt
Reinhardt9mo ago
alright, item by item then public void enterSales, "the modifier public is not valid for this item"
Jimmacle
Jimmacle9mo ago
right so in C# you can't have "free functions" methods must be defined inside a class
MODiX
MODiX9mo ago
Angius
It seems like GetItemQuantity method is nested within GetItemNum, which makes it local
React with ❌ to remove this embed.
Jimmacle
Jimmacle9mo ago
everything in your CalculateCommission file is not in a class definition, which is probably generating 99% of your errors
Reinhardt
Reinhardt9mo ago
i got the GetItemQuality nesting fixed
Angius
Angius9mo ago
Always keep $structure in mind
MODiX
MODiX9mo ago
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;
protected double protectedField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;
protected double protectedField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
Angius
Angius9mo ago
Methods go into classes Besides the one and only Program.cs file that can contain top-level code
Reinhardt
Reinhardt9mo ago
"a namespace cannot directly contain members such as fields, methods or statements" on line 6
Jimmacle
Jimmacle9mo ago
yes, it's telling you the same thing we are
Reinhardt
Reinhardt9mo ago
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
Jimmacle
Jimmacle9mo ago
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 that
Reinhardt
Reinhardt9mo ago
no, 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(); }
Jimmacle
Jimmacle9mo ago
well, did you make a namespace or a class named CalculateCommission? that code is expecting a class with that name
Reinhardt
Reinhardt9mo ago
both
namespace CalculateCommission;
public class CalculateCommission { private decimal totalSales = 0;
in CalculateCommission.cs
Jimmacle
Jimmacle9mo ago
all your files should probably use the same namespace for simplicity's sake generally namespaces follow the folder structure of the project
Reinhardt
Reinhardt9mo ago
okay using namespace CalculateComission on StartApp worked, thank you oh i guess program.cs needs a main method?
Jimmacle
Jimmacle9mo ago
you need a main method somewhere in your project, it doesn't have to be in that specific file $mains
MODiX
MODiX9mo ago
The possible signatures for Main are
public static void Main() { }
public static int Main() { }
public static void Main(string[] args) { }
public static int Main(string[] args) { }
public static async Task Main() { }
public static async Task<int> Main() { }
public static async Task Main(string[] args) { }
public static async Task<int> Main(string[] args) { }
public static void Main() { }
public static int Main() { }
public static void Main(string[] args) { }
public static int Main(string[] args) { }
public static async Task Main() { }
public static async Task<int> Main() { }
public static async Task Main(string[] args) { }
public static async Task<int> Main(string[] args) { }
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-line
Main() and command-line arguments - C#
Learn about Main() and command-line arguments. The 'Main' method is the entry point of an executable program.
Jimmacle
Jimmacle9mo ago
that's how your computer knows where to start running your program
Reinhardt
Reinhardt9mo ago
"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(); }
Jimmacle
Jimmacle9mo ago
remember what we talked about re: where you're allowed to define methods
Reinhardt
Reinhardt9mo ago
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?
Jimmacle
Jimmacle9mo ago
first, fix the indentation so we can read it without crying when code using $code
MODiX
MODiX9mo ago
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.
Reinhardt
Reinhardt9mo ago
$codegif
Reinhardt
Reinhardt9mo ago
'''cs private void CalculateItemSale(int itemQuantity, decimal itemCost) { // code to calculate item sales // code to update totalSales running total while (itemQuantity != -1) {
} } } ''' im crying
Jimmacle
Jimmacle9mo ago
` is the key to the left of 1 on the top row with ~
Reinhardt
Reinhardt9mo ago
private void CalculateItemSale(int itemQuantity, decimal itemCost)
{
// code to calculate item sales
// code to update totalSales running total
while (itemQuantity != -1)
{

}
}
}
private void CalculateItemSale(int itemQuantity, decimal itemCost)
{
// code to calculate item sales
// code to update totalSales running total
while (itemQuantity != -1)
{

}
}
}
Jimmacle
Jimmacle9mo ago
whenever you start a new { } block you should indent what's inside it
Reinhardt
Reinhardt9mo ago
hmm now that i think, i should be able to still use itemNum in this, right?
Jimmacle
Jimmacle9mo ago
well, what is CalculateItemSale supposed to do?
Reinhardt
Reinhardt9mo ago
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
if (itemQuantity != -1)
{
// calculate sales amount based upon the entered item number
// add to totalSales

}
if (itemQuantity != -1)
{
// calculate sales amount based upon the entered item number
// add to totalSales

}
here's the full CalculateCommissions.cs file sadness i cannot
Jimmacle
Jimmacle9mo ago
it looks like EnterSales() already has some basic checks to handle the -1s so your code only has to worry about 1 to 5
Reinhardt
Reinhardt9mo ago
correct, 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?
Jimmacle
Jimmacle9mo ago
yeah
Reinhardt
Reinhardt9mo ago
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
Jimmacle
Jimmacle9mo ago
where is itemNum currently defined?
Reinhardt
Reinhardt9mo ago
or both definied in public void EnterSales
Angius
Angius9mo ago
$scopes
MODiX
MODiX9mo ago
scope A {
thing a;
scope B {
thing b;
}
}
scope A {
thing a;
scope B {
thing b;
}
}
thing a is available in scope A and scope B thing b is available only in scope B
Angius
Angius9mo ago
This will let you decide where to put what
Reinhardt
Reinhardt9mo ago
public void EnterSales()
{
int itemNum = 0; // local method variables
int itemQuantity = 0;

private void CalculateItemSale(int itemQuantity, decimal itemCost)
{
// code to calculate item sales
// code to update totalSales running total
while (itemQuantity != -1)
{
if (itemNum == 1)
{

}
}
}
}
public void EnterSales()
{
int itemNum = 0; // local method variables
int itemQuantity = 0;

private void CalculateItemSale(int itemQuantity, decimal itemCost)
{
// code to calculate item sales
// code to update totalSales running total
while (itemQuantity != -1)
{
if (itemNum == 1)
{

}
}
}
}
only itemNum is giving me an error
Jimmacle
Jimmacle9mo ago
you shouldn't be nesting methods in other methods
Reinhardt
Reinhardt9mo ago
its not, just for ease of showing
Jimmacle
Jimmacle9mo ago
it doesn't make reading it easy at all beacuse it changes how your code works
Reinhardt
Reinhardt9mo ago
beginning and end of the file, too big to send the whole thing
Jimmacle
Jimmacle9mo ago
we can't help based on code that doesn't actually exist that way
Reinhardt
Reinhardt9mo ago
cs
cs
Reinhardt
Reinhardt9mo ago
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
namespace CalculateCommission;


public class CalculateCommission
{
private decimal totalSales = 0;
public void EnterSales()
{
int itemNum = 0; // local method variables
int itemQuantity = 0;

// continue to enter sales information until you enter a -1
while (itemNum != -1 && itemQuantity != -1)
{
itemNum = GetItemNum();

if (itemNum != -1)
{
itemQuantity = GetItemQuantity();

if (itemQuantity != -1)
{
// calculate sales amount based upon the entered item number
// add to totalSales

}
}
}
// calculate sales commission per the book exercise
// print out the total sales to the console
// print out the commission total to the console
// print out the total pay to the console
}
namespace CalculateCommission;


public class CalculateCommission
{
private decimal totalSales = 0;
public void EnterSales()
{
int itemNum = 0; // local method variables
int itemQuantity = 0;

// continue to enter sales information until you enter a -1
while (itemNum != -1 && itemQuantity != -1)
{
itemNum = GetItemNum();

if (itemNum != -1)
{
itemQuantity = GetItemQuantity();

if (itemQuantity != -1)
{
// calculate sales amount based upon the entered item number
// add to totalSales

}
}
}
// calculate sales commission per the book exercise
// print out the total sales to the console
// print out the commission total to the console
// print out the total pay to the console
}
private int GetItemNum()
{
var itemNum = 0;
// code to prompt an item number => Console.Write
// code to read input => Console.ReadLine
// will require a while loop
// must validate input
// will require a int.TryParse to convert input to int
// loop until you receive 1, 2, 3, 4, 5 or -1
// -1 is code to quit entering sales data
// any other numbers are invalid
// NOTE: This method only returns 1, 2, 3, 4, 5 or -1
while (true)
{
Console.Write("Enter item number: ");
string input = Console.ReadLine();

if (!int.TryParse(input, out itemNum))
{
Console.WriteLine("Invalid input. Please enter a valid integer.");
continue;
}
if (itemNum == -1)
{
return -1;
}
else if (itemNum == 1)
{
return 1;
}
else if (itemNum == 2)
{
return 2;
}
else if (itemNum == 3)
{
return 3;
}
else if (itemNum == 4)
{
return 4;
}
else if (itemNum == 5)
{
return 5;
}
else if (itemNum == 6)
{
return 6;
}
return itemNum;
}
}
private int GetItemNum()
{
var itemNum = 0;
// code to prompt an item number => Console.Write
// code to read input => Console.ReadLine
// will require a while loop
// must validate input
// will require a int.TryParse to convert input to int
// loop until you receive 1, 2, 3, 4, 5 or -1
// -1 is code to quit entering sales data
// any other numbers are invalid
// NOTE: This method only returns 1, 2, 3, 4, 5 or -1
while (true)
{
Console.Write("Enter item number: ");
string input = Console.ReadLine();

if (!int.TryParse(input, out itemNum))
{
Console.WriteLine("Invalid input. Please enter a valid integer.");
continue;
}
if (itemNum == -1)
{
return -1;
}
else if (itemNum == 1)
{
return 1;
}
else if (itemNum == 2)
{
return 2;
}
else if (itemNum == 3)
{
return 3;
}
else if (itemNum == 4)
{
return 4;
}
else if (itemNum == 5)
{
return 5;
}
else if (itemNum == 6)
{
return 6;
}
return itemNum;
}
}
private int GetItemQuantity()
{
var itemQuantity = 0;

while (true)
{
Console.Write("Enter item quantity: ");
string input = Console.ReadLine();

if (!int.TryParse(input, out itemQuantity))
{
Console.WriteLine("Invalid input. Please enter a valid integer.");
continue;
}

if (itemQuantity == -1)
{
return -1;
}
else if (itemQuantity > 0)
{
return itemQuantity;
}
else
{
Console.WriteLine("Invalid input. Quantity must be a positive number or -1 to quit.");
}
}
}
private int GetItemQuantity()
{
var itemQuantity = 0;

while (true)
{
Console.Write("Enter item quantity: ");
string input = Console.ReadLine();

if (!int.TryParse(input, out itemQuantity))
{
Console.WriteLine("Invalid input. Please enter a valid integer.");
continue;
}

if (itemQuantity == -1)
{
return -1;
}
else if (itemQuantity > 0)
{
return itemQuantity;
}
else
{
Console.WriteLine("Invalid input. Quantity must be a positive number or -1 to quit.");
}
}
}
private void CalculateItemSale(int itemQuantity, decimal itemCost)
{
// code to calculate item sales
// code to update totalSales running total
while (itemQuantity != -1)
{
if (itemNum == 1)
{

}
}
}
}
private void CalculateItemSale(int itemQuantity, decimal itemCost)
{
// code to calculate item sales
// code to update totalSales running total
while (itemQuantity != -1)
{
if (itemNum == 1)
{

}
}
}
}
Jimmacle
Jimmacle9mo ago
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
Reinhardt
Reinhardt9mo ago
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
Jimmacle
Jimmacle9mo ago
KEKW you know that you can compare numbers with > and <?
Reinhardt
Reinhardt9mo ago
yes
Reinhardt
Reinhardt9mo ago
this is our 'learning material' for this unit, 4 slides of code
Jimmacle
Jimmacle9mo ago
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
Reinhardt
Reinhardt9mo ago
exactly
Jimmacle
Jimmacle9mo ago
for GetItemNum() for example, what happens if i type a 7
Reinhardt
Reinhardt9mo ago
it should give you "Invalid input"
Jimmacle
Jimmacle9mo ago
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 types
Reinhardt
Reinhardt9mo ago
so i need to add one in at the end for an else if itemNum >= 6?
Jimmacle
Jimmacle9mo ago
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)
SinFluxx
SinFluxx9mo ago
It will need checking in the while condition won't it, because they're supposed to keep looping until they get valid input?
Jimmacle
Jimmacle9mo ago
yes, but the existing checks are already in the loop
SinFluxx
SinFluxx9mo ago
Ah I thought you were recommending to get rid of the current loop body
Jimmacle
Jimmacle9mo ago
nah, just the chain of if statements this whole part
if (itemNum == -1)
{
return -1;
}
else if (itemNum == 1)
{
return 1;
}
else if (itemNum == 2)
{
return 2;
}
else if (itemNum == 3)
{
return 3;
}
else if (itemNum == 4)
{
return 4;
}
else if (itemNum == 5)
{
return 5;
}
else if (itemNum == 6)
{
return 6;
}
return itemNum;
if (itemNum == -1)
{
return -1;
}
else if (itemNum == 1)
{
return 1;
}
else if (itemNum == 2)
{
return 2;
}
else if (itemNum == 3)
{
return 3;
}
else if (itemNum == 4)
{
return 4;
}
else if (itemNum == 5)
{
return 5;
}
else if (itemNum == 6)
{
return 6;
}
return itemNum;
is effectively simply
return itemNum;
return itemNum;
SinFluxx
SinFluxx9mo ago
Yeah, so I assumed scrap that, move the return outside the while loop, change while condition
Jimmacle
Jimmacle9mo ago
yeah the loop could be restructured but it's fine for now
Reinhardt
Reinhardt9mo ago
so what should i be doing?
Jimmacle
Jimmacle9mo ago
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
Reinhardt
Reinhardt9mo ago
okay thats what im asking for help with like should i just change each one to >= 1, >=2, etc?
Jimmacle
Jimmacle9mo ago
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?
Reinhardt
Reinhardt9mo ago
wouldnt it just be this
if (studentGrade >= 90)
{
Console.WriteLine("A");
}
else if (studentGrade >= 80)
{
Console.WriteLine("B");
}
else if (studentGrade >= 70)
{
Console.WriteLine("C");
}
else if (studentGrade >= 60)
{
Console.WriteLine("D");
}
else
{
Console.WriteLine("F");
}
if (studentGrade >= 90)
{
Console.WriteLine("A");
}
else if (studentGrade >= 80)
{
Console.WriteLine("B");
}
else if (studentGrade >= 70)
{
Console.WriteLine("C");
}
else if (studentGrade >= 60)
{
Console.WriteLine("D");
}
else
{
Console.WriteLine("F");
}
but 5/4/3/2/1
Jimmacle
Jimmacle9mo ago
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
Reinhardt
Reinhardt9mo ago
well -1 is valid but 0 isn't
Jimmacle
Jimmacle9mo ago
right, that's a different case that you have to handle
MODiX
MODiX9mo ago
Jimmacle
so you want to check if a number equals -1 or is 1 or higher and 5 or lower
React with ❌ to remove this embed.
Reinhardt
Reinhardt9mo ago
so would it be better to just do an if for each case then?
Jimmacle
Jimmacle9mo ago
pretty much except for the higher and lower checks you have to make sure they're both true
Reinhardt
Reinhardt9mo ago
and then just make another for like, >=6, =0, and <=-2?
Jimmacle
Jimmacle9mo ago
for example, 7 is bigger than 1 but it's not smaller than 5 so it should be invalid
Reinhardt
Reinhardt9mo ago
yeah just do three more ifs with error statements
Jimmacle
Jimmacle9mo ago
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
Reinhardt
Reinhardt9mo ago
got it so i just do if's for the ones i want, and else accounts for all other cases
Jimmacle
Jimmacle9mo ago
right
Reinhardt
Reinhardt9mo ago
so for this
private int GetItemNum()
{
var itemNum = 0;
// code to prompt an item number => Console.Write
// code to read input => Console.ReadLine
// will require a while loop
// must validate input
// will require a int.TryParse to convert input to int
// loop until you receive 1, 2, 3, 4, 5 or -1
// -1 is code to quit entering sales data
// any other numbers are invalid
// NOTE: This method only returns 1, 2, 3, 4, 5 or -1
while (true)
{
Console.Write("Enter item number: ");
string input = Console.ReadLine();

if (!int.TryParse(input, out itemNum))
{
Console.WriteLine("Invalid input. Please enter a valid integer.");
continue;
}
if (itemNum == -1)
{
return -1;
}
else if (itemNum == 1)
{
return 1;
}
else if (itemNum == 2)
{
return 2;
}
else if (itemNum == 3)
{
return 3;
}
else if (itemNum == 4)
{
return 4;
}
else if (itemNum == 5)
{
return 5;
}
return itemNum;
}
}
private int GetItemNum()
{
var itemNum = 0;
// code to prompt an item number => Console.Write
// code to read input => Console.ReadLine
// will require a while loop
// must validate input
// will require a int.TryParse to convert input to int
// loop until you receive 1, 2, 3, 4, 5 or -1
// -1 is code to quit entering sales data
// any other numbers are invalid
// NOTE: This method only returns 1, 2, 3, 4, 5 or -1
while (true)
{
Console.Write("Enter item number: ");
string input = Console.ReadLine();

if (!int.TryParse(input, out itemNum))
{
Console.WriteLine("Invalid input. Please enter a valid integer.");
continue;
}
if (itemNum == -1)
{
return -1;
}
else if (itemNum == 1)
{
return 1;
}
else if (itemNum == 2)
{
return 2;
}
else if (itemNum == 3)
{
return 3;
}
else if (itemNum == 4)
{
return 4;
}
else if (itemNum == 5)
{
return 5;
}
return itemNum;
}
}
i just replace return itemNum with else { error message }
Jimmacle
Jimmacle9mo ago
yep you don't want to return there because at that point you know the number isn't a valid one
Reinhardt
Reinhardt9mo ago
yeah
Jimmacle
Jimmacle9mo ago
if you wanted to make the method a lot shorter you could turn the whole if chain into
if (itemNum == -1 || (itemNum >= 1 && itemNum <= 5))
{
return itemNum;
}
if (itemNum == -1 || (itemNum >= 1 && itemNum <= 5))
{
return itemNum;
}
Reinhardt
Reinhardt9mo ago
now i have the same two errors a bunch
private void CalculateItemSale(int itemQuantity, decimal itemCost)
{
decimal itemCost = 0;
// code to calculate item sales
// code to update totalSales running total
while (itemQuantity != -1)
{
if (itemNum == 1)
{
var itemCost = 5.99;
}
else if (itemNum == 2)
{
var itemCost = 19.99;
}
else if (itemNum == 3)
{
var itemCost = 59.99;
}
else if (itemNum == 4)
{
var itemCost = 99.99;
}
else if (itemNum == 5)
{
var itemCost = 199.99;
}
}
}
private void CalculateItemSale(int itemQuantity, decimal itemCost)
{
decimal itemCost = 0;
// code to calculate item sales
// code to update totalSales running total
while (itemQuantity != -1)
{
if (itemNum == 1)
{
var itemCost = 5.99;
}
else if (itemNum == 2)
{
var itemCost = 19.99;
}
else if (itemNum == 3)
{
var itemCost = 59.99;
}
else if (itemNum == 4)
{
var itemCost = 99.99;
}
else if (itemNum == 5)
{
var itemCost = 199.99;
}
}
}
"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"
Jimmacle
Jimmacle9mo ago
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?
Reinhardt
Reinhardt9mo ago
yes, which item it is, because the items have different prices
Jimmacle
Jimmacle9mo ago
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 number
Reinhardt
Reinhardt9mo ago
well the price of each item isn't stored anywhere else this method is the only place the variable itemCost is even mentioned
Jimmacle
Jimmacle9mo ago
it must be stored somewhere else, because whatever calls this method has to pass in a cost
Reinhardt
Reinhardt9mo ago
would i put it in the GetItemNum method? i pasted the entire code starting here https://discord.com/channels/143867839282020352/1158482223801053244/1158500197500997642
MODiX
MODiX9mo ago
Reinhardt
namespace CalculateCommission;


public class CalculateCommission
{
private decimal totalSales = 0;
public void EnterSales()
{
int itemNum = 0; // local method variables
int itemQuantity = 0;

// continue to enter sales information until you enter a -1
while (itemNum != -1 && itemQuantity != -1)
{
itemNum = GetItemNum();

if (itemNum != -1)
{
itemQuantity = GetItemQuantity();

if (itemQuantity != -1)
{
// calculate sales amount based upon the entered item number
// add to totalSales

}
}
}
// calculate sales commission per the book exercise
// print out the total sales to the console
// print out the commission total to the console
// print out the total pay to the console
}
namespace CalculateCommission;


public class CalculateCommission
{
private decimal totalSales = 0;
public void EnterSales()
{
int itemNum = 0; // local method variables
int itemQuantity = 0;

// continue to enter sales information until you enter a -1
while (itemNum != -1 && itemQuantity != -1)
{
itemNum = GetItemNum();

if (itemNum != -1)
{
itemQuantity = GetItemQuantity();

if (itemQuantity != -1)
{
// calculate sales amount based upon the entered item number
// add to totalSales

}
}
}
// calculate sales commission per the book exercise
// print out the total sales to the console
// print out the commission total to the console
// print out the total pay to the console
}
React with ❌ to remove this embed.
Jimmacle
Jimmacle9mo ago
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
Reinhardt
Reinhardt9mo ago
// 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
Jimmacle
Jimmacle9mo ago
no it doesn't look at this part private void CalculateItemSale(int itemQuantity, decimal itemCost) what data is being provided to the method?
Reinhardt
Reinhardt9mo ago
the number of items, and the cost of the item
Jimmacle
Jimmacle9mo ago
right so why do you need the item number to find the cost if you already have the cost of the item?
Reinhardt
Reinhardt9mo ago
because there are 5 different possible costs
Jimmacle
Jimmacle9mo ago
but you don't care here whatever cost you need is provided in the itemCost variable already
Reinhardt
Reinhardt9mo ago
which isnt stored anywhere else but this method
Jimmacle
Jimmacle9mo ago
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?
Reinhardt
Reinhardt9mo ago
yes
Jimmacle
Jimmacle9mo ago
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
Reinhardt
Reinhardt9mo ago
private void CalculateItemSale(int itemQuantity, decimal itemCost)
{
decimal itemCost = 0;
// code to calculate item sales
// code to update totalSales running total
totalSales = itemQuantity * itemCost;
}
private void CalculateItemSale(int itemQuantity, decimal itemCost)
{
decimal itemCost = 0;
// code to calculate item sales
// code to update totalSales running total
totalSales = itemQuantity * itemCost;
}
now just gives me one of the 'a local or parameter' errors
Jimmacle
Jimmacle9mo ago
yes, because you are trying to declare itemCost but it's already declared in the method arguments
Reinhardt
Reinhardt9mo ago
on the decimal itemCost that's part of the starter code er, no
Jimmacle
Jimmacle9mo ago
No description
Reinhardt
Reinhardt9mo ago
okay so then am i putting the individual prices for the numbers in
if (itemQuantity != -1)
{
// calculate sales amount based upon the entered item number
// add to totalSales

}
if (itemQuantity != -1)
{
// calculate sales amount based upon the entered item number
// add to totalSales

}
Jimmacle
Jimmacle9mo ago
that makes sense to me
Reinhardt
Reinhardt9mo ago
and would i do it the way i had it before and just invoke CalculateItemSale
Jimmacle
Jimmacle9mo ago
right
SinFluxx
SinFluxx9mo ago
Doesn't that total sales calculation need correcting first?
Reinhardt
Reinhardt9mo ago
?
Jimmacle
Jimmacle9mo ago
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 previously
Reinhardt
Reinhardt9mo ago
so i can just do
if (intNum == 1)
{
itemCost = 5.99;
}
if (intNum == 1)
{
itemCost = 5.99;
}
and so on? inside of
if (itemQuantity != -1)
{
// calculate sales amount based upon the entered item number
// add to totalSales

}
if (itemQuantity != -1)
{
// calculate sales amount based upon the entered item number
// add to totalSales

}
Jimmacle
Jimmacle9mo ago
yeah basically you figure out the item cost there, then pass it in when you call CalculateItemSale
Reinhardt
Reinhardt9mo ago
nope 'the name itemCost does not exist in the current context
Jimmacle
Jimmacle9mo ago
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 in
Reinhardt
Reinhardt9mo ago
okay i dont have any errors but it fails to build
Reinhardt
Reinhardt9mo ago
No description
Jimmacle
Jimmacle9mo ago
it looks like your program is still running in the background somewhere
Reinhardt
Reinhardt9mo ago
does it work though? might just be my computer
Jimmacle
Jimmacle9mo ago
it is just your computer you have to stop the program from running before you can rebuild it
Reinhardt
Reinhardt9mo ago
it doesnt seem to be running on my end?
Jimmacle
Jimmacle9mo ago
that error only happens if it is if in doubt restart pc
Reinhardt
Reinhardt9mo ago
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
Jimmacle
Jimmacle9mo ago
idk, i didn't try it you should test and make sure it works
Reinhardt
Reinhardt9mo ago
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
Accord
Accord9mo 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.