0

In my problem, I am trying to output a Sales Summary for an employees commission on sales. One of the columns needed is "Sold" which requires you to get the amount of products sold for that specific product. The problem I am having is that numberSold variable is initialized in the While loop and it looks like I am not able to use it because of that. What is the best option for being able to be able to use it. Any other tips on how to make my CommEarnings cleaner would also be appreciated as I feel like there is a better way to do it compared to how I am getting it.

decimal grossSales = 0; // total gross sales
    decimal earnings; // earnings made from sales
    int product = 0; // the product number
    int numberSold; // number sold of a given product
    decimal commission1 = .09M;
    decimal commission2 = .0925M;
    decimal commission3 = .0701M;
    decimal commission4 = .095M;
    decimal commission5 = .10M;
    decimal commission6 = .083M;
    decimal basePay = 250.00M;
    decimal product1CommEarnings = 0.00M;
    decimal product2CommEarnings = 0.00M;
    decimal product3CommEarnings = 0.00M;
    decimal product4CommEarnings = 0.00M;
    decimal product5CommEarnings = 0.00M;
    decimal product6CommEarnings = 0.00M;
    
    while ( product < 6 )
  {
     ++product;

     // prompt for and read number of the product sold from user
     Console.Write( "Enter number sold of product # {0} ->   ",
        product );
     numberSold = Convert.ToInt32( Console.ReadLine() );

        // determine gross of each individual product and add to total
        if (product == 1)
        {
            grossSales += numberSold * 239.99M;
            product1CommEarnings = Math.Round(commission1 * numberSold * 239.99M, 2);

        }
        else if (product == 2)
        {
            grossSales += numberSold * 129.75M;
            product2CommEarnings = Math.Round(commission2 * numberSold * 129.75M, 2);
        }

        else if (product == 3)
        {
            grossSales += numberSold * 99.95M;
            product3CommEarnings = Math.Round(commission3 * numberSold * 99.95M, 2);
        }

        else if (product == 4)
        {
            grossSales += numberSold * 350.89M;
            product4CommEarnings = Math.Round(commission4 * numberSold * 350.89M, 2);
        }
        else if (product == 5)
        {
            grossSales += numberSold * 100.00M;
            product5CommEarnings = Math.Round(commission5 * numberSold * 100.00M, 2);
        }
        else if (product == 6)
        {
            grossSales += numberSold * 1000.00M;
            product6CommEarnings = Math.Round(commission6 * numberSold * 1000.00M, 2);
        }
    } // end while loop

    Console.WriteLine("\n");
    Console.WriteLine("                   Commission                 Price                   Comm");
    Console.WriteLine("  Sales Summary      Earnings     Sold         Each    Extentsion     Rate");
    Console.WriteLine("  -------------    ----------     ----        -----     ---------     ----");
    Console.WriteLine($"  Base Pay            {basePay:C}                                        ");
    Console.WriteLine($"  Product 1             {product1CommEarnings}                                                      ");
    Console.WriteLine($"  Product 2             {product2CommEarnings}                                                      ");
    Console.WriteLine($"  Product 3             {product3CommEarnings}                                                      ");
    Console.WriteLine($"  Product 4            {product4CommEarnings}                                                       ");
    Console.WriteLine($"  Product 5             {product5CommEarnings}                                                      ");
    Console.WriteLine($"  Product 6            {product6CommEarnings}                                                       ");
Avery O
  • 63
  • 5
  • 4
    Q: Is there a way for me to output a variable that is initialized in a loop? A: Sure. Just declare the variable *outside* of the loop. Q: Is there a specific problem with the code you've shown? – paulsm4 Oct 23 '20 at 19:04
  • 1
    `numberSold` *is* declared outside the loop with the other vars, so if you are getting an error you should share that so we can work on solving the correct problem – Ňɏssa Pøngjǣrdenlarp Oct 23 '20 at 19:10
  • Not sure how I missed it being declared outside of the loop, looks like all I needed to do was assign to the variable. Sorry about that and thank you for the help. – Avery O Oct 23 '20 at 19:13
  • 1
    ALSO: Please consider using a [switch block](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch), instead of a bunch of "if/else" statements. And *definitely* ensure you *always* detect and handle the "none of the above" case! What if your biggest seller for the month was "Product 7"? ;) – paulsm4 Oct 23 '20 at 19:18
  • 1
    @paulsm4 what's wrong with if/else – Muhammad Waqas Aziz Oct 23 '20 at 19:24
  • "Honey, how come you have to ask me that?" - Bob Dylan. If/else statements are often perfectly appropriate ... but a "switch/case" block should be preferred, whenever possible. The difference is basically "structured" vs. "unstructured". Here are a couple of links; you can easily Google for many more: [Why is it a bad programming practice to use if/else?](https://www.quora.com/Why-is-it-a-bad-programming-practice-to-use-if-else), [Why is the 'if' statement considered evil?](https://stackoverflow.com/a/1554691/421195). And failure to detect "none of the above" is almost always "wrong". – paulsm4 Oct 23 '20 at 19:56
  • The conditional can be cleaned up using a lookup dictionary> which will be easy to extend. – Pramod B R Jan 22 '21 at 09:30

1 Answers1

2

You should initialize the numberSold.

int numberSold = 0; //initial value will be discarded by the loop.

But you are still not going to get the numberSold for each item as you are rewriting it in the loop and the value being printed will be the value from the last iteration of the loop.

Use a record class or a tuple to save the values from each iteration.

Pramod B R
  • 612
  • 6
  • 7