-1

I am trying to pull the total from the program and putting it into an array to be displayed when the program is closed but the current code has a problem as it takes the last number inputted and squashes it together with the number in array slot 0. How do I fix this?

string[] Sales = new string[5];
int i = 0;
string Invoice = "";
int numberOfInvoices = 0;
decimal totalOfInvoices = 0m;
decimal invoiceAverage = 0m;
string Total = "";

private void btnCalculate_Click(object sender, EventArgs e)
{
    //sets the variables
    decimal subtotal = Convert.ToDecimal(txtEnterSubtotal.Text);
    decimal discountPercent = .25m;
    decimal discountAmount = Math.Round(subtotal * discountPercent, 2);
    decimal invoiceTotal = subtotal - discountAmount;

    //retrieves the data from the form
    txtSubtotal.Text = subtotal.ToString("c");
    txtDiscountPercent.Text = discountPercent.ToString("p1");
    txtDiscountAmount.Text = discountAmount.ToString("c");
    txtTotal.Text = invoiceTotal.ToString("c");

    //preforms the various math functions
    numberOfInvoices++;
    totalOfInvoices += invoiceTotal;
    invoiceAverage = totalOfInvoices / numberOfInvoices;

    Total = Convert.ToString(invoiceTotal);
    for (int i = 0; i < Sales.Length; i++) ;
    {
        Sales[i] += Total + "\n";
    }

    txtNumberOfInvoices.Text = numberOfInvoices.ToString();
    txtTotalOfInvoices.Text = totalOfInvoices.ToString("c");
    txtInvoiceAverage.Text = invoiceAverage.ToString("c");

    txtEnterSubtotal.Text = "";

    //sets the focus on Enter Subtotal
    txtEnterSubtotal.Focus();

}
private void btnClearTotals_Click(object sender, EventArgs e)
{
    numberOfInvoices = 0;
    totalOfInvoices = 0m;
    invoiceAverage = 0m;

    txtNumberOfInvoices.Text = "";
    txtTotalOfInvoices.Text = "";
    txtInvoiceAverage.Text = "";

    txtEnterSubtotal.Focus();
}
private void btnExit_Click(object sender, EventArgs e)
{
    for (int i = 0; i < 5; i++) ;
    { 
    foreach (string Tot in Sales)

        {
            Total += Tot;
        }
    }
    MessageBox.Show(Total, "Invoices");
    //closes the program
    this.Close();
}

This is what I get when I enter 100, 200, 300, 400, and 500.

375.0075.00, 150.00, 225.00, 300.00, 375.00

MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • use a List to store the values of the totals along with the `string.Join()` method and it will work – MethodMan Mar 27 '17 at 22:09
  • Indeed, do not use an array. Use a list, start it as empty, and add an element to it. It will automatically resize the list and add the new item at the end. – Eric Lippert Mar 27 '17 at 22:15

1 Answers1

0

I would use a list...

Creating a list

var Sales = new List<int>();

Then to add to the list

Sales.Add(total);

You can keep adding to the list.

Looks like you're converting numbers to strings needlessly, from the looks of it just work with decimals and ints.

Hope this helps.

Bossman
  • 1,416
  • 1
  • 11
  • 17
  • it has to stay an array if it weren't for that requirement then I would use a list. – caleb rice Mar 28 '17 at 00:17
  • If you NEED to keep it for whatever reason... Maybe you can convert your array to a list and work with it? http://stackoverflow.com/questions/1603170/conversion-of-system-array-to-list – Bossman Mar 28 '17 at 13:00