0

Code is to solve a problem w/ company of 4 saleman that sell 5 items and once a day each onputs a dollar value of that product sold for the day.

For some reason the compiler states the index is out of bounds.

public class TotalSales
{

   public static void Main(string[] args)
   {
      const int PEOPLE = 5; // # rows
      const int PRODUCTS = 6; // # columns

      decimal[,] sales;
      sales = new decimal[PEOPLE, PRODUCTS];
      decimal value, totalSales;
      decimal[] productSales = { 0 };

      int salesPerson;
      int product;


      Console.Write("Enter the salesperson (1-4). enter -1 to end input: ");
      salesPerson = Convert.ToInt32(Console.ReadLine());

      while (salesPerson != -1)
      {
         Console.Write("Enter the product number (1-5): ");
         product = Convert.ToInt32(Console.ReadLine());

         Console.Write("Enter total sales: ");
         value = Convert.ToDecimal(Console.ReadLine());

         sales[salesPerson, product] += value;

         Console.WriteLine(" ");

         Console.Write("Enter the salesperson (1-4). enter -1 to end input: ");
         salesPerson = Convert.ToInt32(Console.ReadLine());
      }

      Console.WriteLine("\n");
      Console.WriteLine("The total sales for each salesperson are displayed");
      Console.WriteLine("at the end of each row and the total sales for");
      Console.WriteLine("each product are displayed at the bottom of each column.");

      for (int i = 0; i < 5; i++)
      {
         totalSales = (decimal)0.0;
         Console.Write("{0}", i);

         for (int j = 0; j < 6; j++)
         {
            totalSales += sales[i, j];
            Console.Write("sales: {0}", sales[i, j]);

            Console.WriteLine("productSales: {0}",productSales[j]);
            Console.WriteLine("sales: {0}",sales[i,j]);
            [inlinecode]productSales[j] += sales[i,j];[/inlinecode]
         }

             Console.WriteLine("{0}", totalSales);

            } // end for loop

            //Console.Write("\nTotal {0}", productSales[1]);

            for (int j =1; j < PRODUCTS; j++)
               Console.Write("{0}",productSales[j]);

            Console.WriteLine("\n");
            return;
      } // end Main

   } // end class

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by pygmalion
0

if you use constansts (which are good for reusability), try to use them everywhere in your code, else they lose their advantage.

plus, if you state in your goal that we're talking about 4 employees and 5 items, why do you increment them by one in your code?

const int PEOPLE = 5;
const int PRODUCTS = 6;

hope this helped
pygmalion

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.