loading the array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Karrill29
    New Member
    • Feb 2015
    • 1

    loading the array

    when i hit the calculate button the number entered into the text box is calculate and then stored in an array (the array has five spaces allotted for the information )
    after you have your five entries into the array you hit exit and it spits out in a message box the contence of the Array .(this should be all five calculated totals in the array)
    what i am experiencing when i hit exit it displays the last total for all of the array no matter the other numbers entered
    here is my code :


    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace InvoiceTotal
    {
        // This is the starting point for exercise 8-1 from
        // "Murach's C# 2010" by Joel Murach
        // (c) 2010 by Mike Murach & Associates, Inc. 
        // www.murach.com
    
        public partial class frmInvoiceTotal : Form
    	{
            
            
            
            
    		public frmInvoiceTotal()
    		{
    			InitializeComponent();
                invoiceTotals[0] = 0.0m;
                invoiceTotals[1] = 0.0m;
                invoiceTotals[2] = 0.0m;
                invoiceTotals[3] = 0.0m;
                invoiceTotals[4] = 0.0m;
    		}
    
            // TODO: declare class variables for array and list here
                decimal[] invoiceTotals = new decimal [5];
           
                
                            
            
            
    
            private void btnCalculate_Click(object sender, EventArgs e)
    		{ 
                try
                {
    
                    if (txtSubtotal.Text == "")
                    {
                        MessageBox.Show(
                            "Subtotal is a required field.", "Entry Error");
                    }
                    else
                    {
                        decimal subtotal = Decimal.Parse(txtSubtotal.Text);
                        if (subtotal > 0 && subtotal < 10000)
                        {
                            decimal discountPercent = 0m;
                            if (subtotal >= 500)
                                discountPercent = .2m;
                            else if (subtotal >= 250 & subtotal < 500)
                                discountPercent = .15m;
                            else if (subtotal >= 100 & subtotal < 250)
                                discountPercent = .1m;
                            decimal discountAmount = subtotal * discountPercent;
                            decimal invoiceTotal = subtotal - discountAmount;
    
                            discountAmount = Math.Round(discountAmount, 2);
                            invoiceTotal = Math.Round(invoiceTotal, 2);
    
                            txtDiscountPercent.Text = discountPercent.ToString("p1");
                            txtDiscountAmount.Text = discountAmount.ToString("c");
                            txtTotal.Text = invoiceTotal.ToString("c");
    
                         
                            // TODO:  Add invoice total to the array here
    
                           for (int i = 0; i < invoiceTotals.Length; i++)
                            {
                            invoiceTotals[i] = invoiceTotal;
                           
                             }
                            
                            
                        }
                        else
                        {
                            MessageBox.Show(
                                "Subtotal must be greater than 0 and less than 10,000.",
                                "Entry Error");
                        }
                    }
                }
                catch (FormatException)
                {
                    MessageBox.Show(
                        "Please enter a valid number for the Subtotal field.",
                        "Entry Error");
                }
                txtSubtotal.Focus();
            }
    
    		private void btnExit_Click(object sender, EventArgs e)
    		{
                // TODO: add code that displays dialog boxes here
        
                string currentTotals = "";
                for (int i = 0; i < invoiceTotals.Length; i++)
                    currentTotals += invoiceTotals[i].ToString("c") + "\n";
                MessageBox.Show(currentTotals, "Order Totals");
    
              
                    this.Close();
    		}
    
            private void frmInvoiceTotal_Load(object sender, EventArgs e)
            {
    
            }
    
    	}
    }
  • iace
    New Member
    • Feb 2015
    • 1

    #2
    You are trying to concatenate a string value, in the below code that you have written.

    Code:
    string currentTotals = "";
    If you really want to display the total. Try parsing it, or else instead of using string use decimal.

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      The problem is line 78. You assign the same value to every element in your array.

      Comment

      Working...