0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MonthlySales
{
    class Program
{
    static void Main(string[] args)
    {
        int num;
        double[] sales = {0};
        Inputs(out num, sales);
        CLASS obj = new CLASS();
        obj.setNumMonths(num);
        obj.setSales(sales);
        obj.Process();
        Outputs(obj);
        Console.Read();

    }

    public static void Inputs(out int a, params double[] b)
    {
        Console.Write("Enter number of sales: ");
        a = Convert.ToInt32(Console.ReadLine());

        for (int i = 0; i < a; i++)
        {
            Console.Write("Please enter sale #{0}: ", i + 1);
            b[i] = Convert.ToDouble(Console.ReadLine());
        }
    }

    public static void Outputs(CLASS obj)
    {
        for (int i = 0; i < obj.getNumMonths(); i++)
        {
            Console.WriteLine("Sale # {0} was {1:C2} and contributed {2:P2}", i + 1, obj.getSales()[i], obj.prop_Contrib);
        }
        Console.WriteLine("Total sum of sales is {0:C2}", obj.prop_Total);
    }

}

}

CLASS.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MonthlySales
{
class CLASS
{
    int numMonths, i;
    double[] sales;
    double total, contrib;

    public CLASS() { } // def constructor

    public CLASS(int a, double[] b) //constructor
    {
        numMonths = a;
        sales = new double[numMonths];
        sales = b;
    }

    public void setNumMonths(int a) //setter
    {
        numMonths = a;
    }

    public void setSales(double[] a) //setter
    {
        sales = a;
    }

    public int getNumMonths() //getter
    {
        return numMonths;
    }

    public double[] getSales() //getter
    {
        return sales;
    }

    public double prop_Total //property
    {
        get { return total; }
    }

    public double prop_Contrib //property
    {
        get { return contrib; }
    }

    public void Process()
    {
        total = 0;
        for(i=0; i < numMonths; i++)
        {
            total = total + sales[i];
        }
        contrib = sales[i] / total;

    }
}

}

When the program asks for the inputs of the user, it runs well. But when it is time to display the output, the console automatically closes. It throws an error in Inputs(out num, sales);and b[i] = Convert.ToDouble(Console.ReadLine()); ( located in the main program). It says

System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

Any insights would help. Many thanks.

khnkhymkh
  • 21
  • 3
  • When posting a question, please create a code sample which isolates the problem rathern than posting the whole program. The more code you post, the more difficult it is for others to see the mistake. It looks like your double[] array is not initialized: see https://stackoverflow.com/questions/202813/adding-values-to-a-c-sharp-array. – Jodiug Feb 06 '18 at 14:15
  • Did you attach a debugger and step through your code? That should immediately reveal the problem. – Daniel Mann Feb 06 '18 at 14:15
  • Id guess your problem is you make a sales with new double[numMonths] and then set it immediately to {0} only so sales doesnt have a length of numMonths as you expect – BugFinder Feb 06 '18 at 14:16

0 Answers0