-1
namespace TaxCalcuator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        double[] rateArray = { 0.10, 0.15, 0.25, 0.28, 0.33, 0.35 ,0.396 };
        double[] taxableIncomeArray = { 0.0, 9275.0, 37650.0, 91150.0, 190150.0, 413350.0, 415050.0 };
        double[] taxDueArray = { 0.0, 927.50, 5183.75, 18558.75, 46278.75, 119934.75, 120529.75 };

        private void compute_Click(object sender, EventArgs e)

        {
            double income;
            int dependents;
            const double STANDARD_DEDUCTION = 6300;
            const double PERSONAL_EXEMPTION = 4050;
            double AGI;
            double tax;
            double taxDue;
            double rate;
            income = Convert.ToDouble(incomeTextBox.Text);
            dependents = Convert.ToInt32(dependentTextBox.Text);

            AGI = income - (PERSONAL_EXEMPTION + PERSONAL_EXEMPTION* dependents) - STANDARD_DEDUCTION;
            label4.Text = "Adjusted Gross Income:" + AGI.ToString("C");

            int sub =  taxableIncomeArray.Length -1;
            while  (sub >= 0 && AGI < taxableIncomeArray[sub])
            {
                if (AGI < 0)
                    tax = 0;
                --sub;
            }; 
            taxDue = taxDueArray[sub];
                rate = rateArray[sub];

            tax = taxDueArray[sub] + (AGI - taxableIncomeArray[sub]) * rate;
            label5.Text= "Income Tax:" +tax.ToString("C");
          }

        }
    }
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Wilfred Labue
  • 43
  • 3
  • 13
  • 1
    Which line gives the error?? – Mohit S Oct 07 '16 at 03:28
  • 1
    Just spend more time with the debugger, the other skill you need to learn. Do reason through what happens when the while() loop steps through the entire array. – Hans Passant Oct 07 '16 at 03:31
  • thid line gives me errors -- { if (AGI < 0) tax = 0; --sub; }; taxDue = taxDueArray[sub]; rate = rateArray[sub]; tax = taxDueArray[sub] + (AGI - taxableIncomeArray[sub]) * rate; – Wilfred Labue Oct 07 '16 at 03:51

1 Answers1

0

The problem probably lies here:

    while  (sub >= 0 && AGI < taxableIncomeArray[sub])
    {
        if (AGI < 0)
            tax = 0;
        --sub;
    };

The last time this loop executes, sub will start as zero and be decremented to -1. Later on you try to use it to index into the other arrays, which obviously do not have an item at index -1.

Chris Shain
  • 50,833
  • 6
  • 93
  • 125
  • problem is --sub will make it -1 you need proper condition to make sure it doesn't go below 0. since array can't be accessed at -1 index – AllSpark Oct 07 '16 at 03:45
  • i have made sub to be ++sub , but it doesn't work when i want to calculate tax at income of $10000. – Wilfred Labue Oct 07 '16 at 04:09