473,385 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 :


Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace InvoiceTotal
  11. {
  12.     // This is the starting point for exercise 8-1 from
  13.     // "Murach's C# 2010" by Joel Murach
  14.     // (c) 2010 by Mike Murach & Associates, Inc. 
  15.     // www.murach.com
  16.  
  17.     public partial class frmInvoiceTotal : Form
  18.     {
  19.  
  20.  
  21.  
  22.  
  23.         public frmInvoiceTotal()
  24.         {
  25.             InitializeComponent();
  26.             invoiceTotals[0] = 0.0m;
  27.             invoiceTotals[1] = 0.0m;
  28.             invoiceTotals[2] = 0.0m;
  29.             invoiceTotals[3] = 0.0m;
  30.             invoiceTotals[4] = 0.0m;
  31.         }
  32.  
  33.         // TODO: declare class variables for array and list here
  34.             decimal[] invoiceTotals = new decimal [5];
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.         private void btnCalculate_Click(object sender, EventArgs e)
  42.         { 
  43.             try
  44.             {
  45.  
  46.                 if (txtSubtotal.Text == "")
  47.                 {
  48.                     MessageBox.Show(
  49.                         "Subtotal is a required field.", "Entry Error");
  50.                 }
  51.                 else
  52.                 {
  53.                     decimal subtotal = Decimal.Parse(txtSubtotal.Text);
  54.                     if (subtotal > 0 && subtotal < 10000)
  55.                     {
  56.                         decimal discountPercent = 0m;
  57.                         if (subtotal >= 500)
  58.                             discountPercent = .2m;
  59.                         else if (subtotal >= 250 & subtotal < 500)
  60.                             discountPercent = .15m;
  61.                         else if (subtotal >= 100 & subtotal < 250)
  62.                             discountPercent = .1m;
  63.                         decimal discountAmount = subtotal * discountPercent;
  64.                         decimal invoiceTotal = subtotal - discountAmount;
  65.  
  66.                         discountAmount = Math.Round(discountAmount, 2);
  67.                         invoiceTotal = Math.Round(invoiceTotal, 2);
  68.  
  69.                         txtDiscountPercent.Text = discountPercent.ToString("p1");
  70.                         txtDiscountAmount.Text = discountAmount.ToString("c");
  71.                         txtTotal.Text = invoiceTotal.ToString("c");
  72.  
  73.  
  74.                         // TODO:  Add invoice total to the array here
  75.  
  76.                        for (int i = 0; i < invoiceTotals.Length; i++)
  77.                         {
  78.                         invoiceTotals[i] = invoiceTotal;
  79.  
  80.                          }
  81.  
  82.  
  83.                     }
  84.                     else
  85.                     {
  86.                         MessageBox.Show(
  87.                             "Subtotal must be greater than 0 and less than 10,000.",
  88.                             "Entry Error");
  89.                     }
  90.                 }
  91.             }
  92.             catch (FormatException)
  93.             {
  94.                 MessageBox.Show(
  95.                     "Please enter a valid number for the Subtotal field.",
  96.                     "Entry Error");
  97.             }
  98.             txtSubtotal.Focus();
  99.         }
  100.  
  101.         private void btnExit_Click(object sender, EventArgs e)
  102.         {
  103.             // TODO: add code that displays dialog boxes here
  104.  
  105.             string currentTotals = "";
  106.             for (int i = 0; i < invoiceTotals.Length; i++)
  107.                 currentTotals += invoiceTotals[i].ToString("c") + "\n";
  108.             MessageBox.Show(currentTotals, "Order Totals");
  109.  
  110.  
  111.                 this.Close();
  112.         }
  113.  
  114.         private void frmInvoiceTotal_Load(object sender, EventArgs e)
  115.         {
  116.  
  117.         }
  118.  
  119.     }
  120. }
  121.  
Feb 1 '15 #1
2 2283
iace
1
You are trying to concatenate a string value, in the below code that you have written.

Expand|Select|Wrap|Line Numbers
  1. string currentTotals = "";
If you really want to display the total. Try parsing it, or else instead of using string use decimal.
Feb 3 '15 #2
Rabbit
12,516 Expert Mod 8TB
The problem is line 78. You assign the same value to every element in your array.
Feb 3 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Aguilar, James | last post by:
My previous example used the concept of a Shape class heirarchy, so I will continue with that. Suppose I have something like fifty different shapes, and I am trying to instantiate one of them. ...
15
by: Geoff Cox | last post by:
Hello I have following type of code in the header function pre_load_pics() { if (document.images) { var image1 = new Image(400,265); image1.scr = "pic1.jpg";
4
by: Jens Mittag | last post by:
Hi! In my code, I have an array of a structure, which I want to save to a binary file. When the array is just created, everything works fine, but when I change contents of the array, saving...
3
by: Bala | last post by:
Hi, In my page there is 4 dropdown list boxes.when page loading that time i am loading the nearly 3000 items array. so its taking too much time to load. is it other way fill up the dropdown...
1
by: bobmct | last post by:
Fellow PHP'ers; I'm digging myself a hole on this one so I thought it has come to the time when I must ask those who know. I have what should be a simple question for loading, accessing and...
4
by: Miro | last post by:
Im trying to store data as I would in some old language. Old Example //Create an array that holds 3 different variable types TempArray := { { "Hello", 1, False }, { "Goodbye", 123, True } } ...
8
by: jhoff | last post by:
So, I'm in a pickle... Lets say I have a page: http://www.gen2host.com/discgolf/index.html and on this page, there are a list of links. These links will be generated by 5 pieces of data pulled...
3
by: Barkingmadscot | last post by:
I am stuck, i can workout how to remove lines from an array I have loading a text file (a Log), I know which lines a need, but the logs can be upto 30K sometimes bigger. I found trying to...
5
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
vs2008 c# how can I load an array from XML? lets say I have now string arr = {23,32,45,34,21,23,243,45 } I want to read the values from XML. thanks
1
anfetienne
by: anfetienne | last post by:
i have this code below that i made....it loads vars from txt file splits it then puts it into an array....once in an array it the brings the pics in from the array to create thumbnails and a larger...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.