Do While loop not working

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 33
Reputation: rogenie is an unknown quantity at this point 
Solved Threads: 0
rogenie rogenie is offline Offline
Light Poster

Do While loop not working

 
0
  #1
Oct 13th, 2008
Can someone explain to me why this code do not try to perform the loop? It just crashes my program
  1. Private Sub btnCommission_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommission.Click
  2. Dim strComAmount As String
  3. Dim decComAmount As Decimal
  4. Dim decAverageCom As Decimal
  5. Dim decTotalOfAllCom As Decimal = 0D
  6. Dim intNumberOfEntry As Integer
  7.  
  8. 'convert textbox data into decimal
  9. strComAmount = Me.txtCommission.Text
  10. decComAmount = Convert.ToDecimal(strComAmount)
  11.  
  12. 'Initiate the loop
  13. While decComAmount > 0
  14. Me.lstTally.Items.Add(decComAmount)
  15. intNumberOfEntry += 1
  16. decTotalOfAllCom += decComAmount
  17. End While
  18.  
  19. 'Calculate Average
  20. decAverageCom = decTotalOfAllCom / intNumberOfEntry
  21.  
  22. 'Display Total and count
  23.  
  24. Me.lblTotal.Text = decAverageCom.ToString("C")
  25. Me.lblCount.Text = CStr(intNumberOfEntry)
  26. Me.lblAverage.Text = decAverageCom.ToString("C")
  27. If decComAmount < 0 Then
  28. MessageBox.Show("Please enter more than one dollar")
  29. End If
  30.  
  31.  
  32. End Sub
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 9
Reputation: hisheeraz is an unknown quantity at this point 
Solved Threads: 0
hisheeraz hisheeraz is offline Offline
Newbie Poster

Re: Do While loop not working

 
0
  #2
Oct 13th, 2008
the value of ur loop in never decreasing...its always increasing..assign loop counter a value and then decrease it 1 by 1 at the end of the each loop...loop wil stop when the counter value reaches 0
domething like

decComAmount = decComAmount -1
Last edited by hisheeraz; Oct 13th, 2008 at 1:27 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 15
Reputation: Tyrone.Wilson is an unknown quantity at this point 
Solved Threads: 1
Tyrone.Wilson Tyrone.Wilson is offline Offline
Newbie Poster

Re: Do While loop not working

 
0
  #3
Oct 13th, 2008
I think hisheeraz has hit the button. the problem is that you have a test condition which remains true forever and thus sends your program into an infinite loop. this would explain the "crash" as a rule you should always ensure that your test condition has a modification statement within the loop and will have a definite end. otherwise another way to do it is to add a safety net like so:

'Initiate the loop
While decComAmount > 0 And decComAmount < 100(for example)
Me.lstTally.Items.Add(decComAmount)
intNumberOfEntry += 1
decTotalOfAllCom += decComAmount
End While
Last edited by Tyrone.Wilson; Oct 13th, 2008 at 3:54 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 33
Reputation: rogenie is an unknown quantity at this point 
Solved Threads: 0
rogenie rogenie is offline Offline
Light Poster

Re: Do While loop not working

 
0
  #4
Oct 13th, 2008
Hi guys. Thanks. I wanted the user to enter as much data as they want and put it in a listbox. then it will end when the calculate average button is clicked.

Why does it do infinite loop? If I dn't do it that way, then how can I make the user input as much number as they want?
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 305
Reputation: timothybard is an unknown quantity at this point 
Solved Threads: 25
timothybard's Avatar
timothybard timothybard is offline Offline
Posting Whiz

Re: Do While loop not working

 
0
  #5
Oct 13th, 2008
If you want the user to enter as many entries as they want, then you need to allow the user to enter values within the while loop. Try something like this:

  1. Private Sub btnCommission_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommission.Click
  2. Dim strComAmount As String
  3. Dim decComAmount As Decimal
  4. Dim decAverageCom As Decimal
  5. Dim decTotalOfAllCom As Decimal = 0D
  6. Dim intNumberOfEntry As Integer
  7.  
  8. 'Initiate the loop
  9. While decComAmount > 0
  10. strComAmount = InputBox ("Enter a value","Enter value")
  11. decComAmount = Convert.ToDecimal(strComAmount)
  12. Me.lstTally.Items.Add(decComAmount)
  13. intNumberOfEntry += 1
  14. decTotalOfAllCom += decComAmount
  15. End While
  16.  
  17. 'Calculate Average
  18. decAverageCom = decTotalOfAllCom / intNumberOfEntry
  19.  
  20. 'Display Total and count
  21.  
  22. Me.lblTotal.Text = decAverageCom.ToString("C")
  23. Me.lblCount.Text = CStr(intNumberOfEntry)
  24. Me.lblAverage.Text = decAverageCom.ToString("C")
  25. If decComAmount < 0 Then
  26. MessageBox.Show("Please enter more than one dollar")
  27. End If
  28.  
  29.  
  30. End Sub

This code will show an input box for the user to enter data instead of using the textbox. If you want the user to use the textbox, then you need to split your code over two events, such as two command buttons: one for user input and the other to process the data.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 15
Reputation: Tyrone.Wilson is an unknown quantity at this point 
Solved Threads: 1
Tyrone.Wilson Tyrone.Wilson is offline Offline
Newbie Poster

Re: Do While loop not working

 
0
  #6
Oct 14th, 2008
Agreed. the problem seems to be that you are going into the loop with no chance of coming out. I see in the code you gave in the first post you only read once with:

strComAmount = Me.txtCommission.Text
decComAmount = Convert.ToDecimal(strComAmount)

after this you go into the loop. thus I don't see what you are trying to average.

The message box would work well but perhaps with an add amount button to add the number and a finish button to finish reading new numbers.

Otherwise if you really wanted to do the textbox idea then perhaps you should do something like the following (in psudo code)

1.user puts in data

2. if the user hits enter key (i.e. in the textbox_enter routine) the following is done:
-convert the entry to your decimal
-add the entry to an arraylist
-incriment the number of entries
-clear the text box
-re-select the textbox 'This is cos it will loose focus when the enter key is pressed
when the user is done then they can press a "Calculate" button. ' this way you will know when to get ready for another entry and when to calculate.

when you hit "Calculate" you will then do something like:

dim thetotal as decimal

for i=0 to myArraylist.count-1
thetotal = theTotal+myArraylist.item(i).todecimal
next

average = thetotal/(myArraylist.count-1)
again this is probably not that syntactically correct as I am writing this on the fly but the logic is about right.

also don't forget that if the user does not hit enter on their last entry you would not have read that value in. you can easily test this by checking that the textbox is in fact blank. if it isn't then do the textbox enter routine before you do your final calculate to ensure that the last entry is there.

hope this helps you do what you want. let me know if you have any questions.
Last edited by Tyrone.Wilson; Oct 14th, 2008 at 2:58 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 15
Reputation: Tyrone.Wilson is an unknown quantity at this point 
Solved Threads: 1
Tyrone.Wilson Tyrone.Wilson is offline Offline
Newbie Poster

Re: Do While loop not working

 
0
  #7
Oct 14th, 2008
hmmm.... just noticed that it is not your post at the top Rogenie, perhaps you should start your own thread. but my answer above makes some sense in any case.
Last edited by Tyrone.Wilson; Oct 14th, 2008 at 3:03 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC