Next, rate... Do, Loop...

Help turning this Next, rate... code to do, loop... code

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1768 Views - Last Post: 06 August 2007 - 09:21 AM Rate Topic: -----

#1 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Next, rate... Do, Loop...

Post icon  Posted 06 August 2007 - 04:41 AM

Ok so im doing my homework for my online Visual Basics class and I run into a code I can not seem to do right. The homework problem is... "Change the Next, Rate codes into Do, Loop codes." Now i have done most of the work but it is suppose to show the rate for 3yrs 4yrs and 5yrs but its only displaying the 3yr rate... need help please! :blink: Also i already know my code is ugly, im very new and its actually harder then expected hehe.

 
Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click
		'calculate monthly payments on a loan
		'using terms of 3, 4 and 5 years and interest
		'rates of 5% through 10%

		Const termheading As String = _
			"		   3 yrs	   4 yrs	   5 yrs"
		Dim principal As Double
		Dim monthlypayment As Double
		Dim isconverted As Boolean

		Me.xPaymentsLabel.Text = String.Empty
		isconverted = _
			Double.TryParse(Me.xPrincipalTextBox.Text, principal)

		If isconverted Then
			'display the term in the heading
			Me.xPaymentsLabel.Text = termheading _
				& ControlChars.NewLine
			'calculate and display payments
			Dim number As Double = 0.04
			Do While number <= 1.0
				number = number + 0.01
				Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
					& number.ToString("P0") & "   "

				Dim term As Double = 3.0
				Do
					term = term + 1
					monthlypayment = _
						-Financial.Pmt(number / 12.0, _
						term * 12.0, principal)
					Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
						& monthlypayment.ToString("N2") & "	 "

					Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
						& ControlChars.NewLine
				Loop Until term <= 5.0

			Loop

		Else	'principal cannot be converted to a number
			MessageBox.Show("Please re-enter the principal.", _
			"Payment Caluclator", MessageBoxButtons.OK, _
			MessageBoxIcon.Information)

		End If

		Me.xPrincipalTextBox.Focus()
		Me.xPrincipalTextBox.SelectAll()

	End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Next, rate... Do, Loop...

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Next, rate... Do, Loop...

Posted 06 August 2007 - 07:34 AM

I moved your both of your incrementers to below the calculations, this should solve your problem :)
 
Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click
		'calculate monthly payments on a loan
		'using terms of 3, 4 and 5 years and interest
		'rates of 5% through 10%

		Const termheading As String = _
			"		   3 yrs	   4 yrs	   5 yrs"
		Dim principal As Double
		Dim monthlypayment As Double
		Dim isconverted As Boolean

		Me.xPaymentsLabel.Text = String.Empty
		isconverted = _
			Double.TryParse(Me.xPrincipalTextBox.Text, principal)

		If isconverted Then
			'display the term in the heading
			Me.xPaymentsLabel.Text = termheading _
				& ControlChars.NewLine
			'calculate and display payments
			Dim number As Double = 0.04
			Do While number <= 1.0
				
				Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
					& number.ToString("P0") & "   "

				Dim term As Double = 3.0
				Do
					
					monthlypayment = _
						-Financial.Pmt(number / 12.0, _
						term * 12.0, principal)
					Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
						& monthlypayment.ToString("N2") & "	 "

					Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
						& ControlChars.NewLine
					   term = term + 1
				Loop Until term <= 5.0
							  number = number + 0.01
			Loop

		Else	'principal cannot be converted to a number
			MessageBox.Show("Please re-enter the principal.", _
			"Payment Caluclator", MessageBoxButtons.OK, _
			MessageBoxIcon.Information)

		End If

		Me.xPrincipalTextBox.Focus()
		Me.xPrincipalTextBox.SelectAll()

	End Sub



Happy Coding!

This post has been edited by PsychoCoder: 06 August 2007 - 07:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: Next, rate... Do, Loop...

Posted 06 August 2007 - 07:49 AM

really thanks alot. But can you explain what you did exactly and what i had wrong, just for further coding if you dont mind.

also, i just tried your code and its doing the same as mine. the program shows 3yr 4yr and 5yr but with my and your code its only displaying the rates for the 3yrs collumn
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Next, rate... Do, Loop...

Posted 06 August 2007 - 07:51 AM

The simple changes I made have a <----- before them in the code below. Look right below where you started your 2 loops, and just above the 2 loop statements
Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click
		'calculate monthly payments on a loan
		'using terms of 3, 4 and 5 years and interest
		'rates of 5% through 10%

		Const termheading As String = _
			"		   3 yrs	   4 yrs	   5 yrs"
		Dim principal As Double
		Dim monthlypayment As Double
		Dim isconverted As Boolean

		Me.xPaymentsLabel.Text = String.Empty
		isconverted = _
			Double.TryParse(Me.xPrincipalTextBox.Text, principal)

		If isconverted Then
			'display the term in the heading
			Me.xPaymentsLabel.Text = termheading _
				& ControlChars.NewLine
			'calculate and display payments
			Dim number As Double = 0.04
			Do While number <= 1.0
				'number = number + 0.01 <----- You had it here
				Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
					& number.ToString("P0") & "   "

				Dim term As Double = 3.0
				Do
					'term = term + 1 <----- You had it here
					monthlypayment = _
						-Financial.Pmt(number / 12.0, _
						term * 12.0, principal)
					Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
						& monthlypayment.ToString("N2") & "	 "

					Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
						& ControlChars.NewLine
					 term = term + 1 '<---- I moved it here, after all the calculations happened 
				Loop Until term <= 5.0
				number = number + 0.01 '<---- I moved it here after all the work was done
			Loop

		Else	'principal cannot be converted to a number
			MessageBox.Show("Please re-enter the principal.", _
			"Payment Caluclator", MessageBoxButtons.OK, _
			MessageBoxIcon.Information)

		End If

		Me.xPrincipalTextBox.Focus()
		Me.xPrincipalTextBox.SelectAll()

	End Sub


Was This Post Helpful? 0
  • +
  • -

#5 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: Next, rate... Do, Loop...

Posted 06 August 2007 - 07:57 AM

AWWW ic, retard me. it should be the last thing before the loop. its the simple things i miss. but the code still is not working, im still messing with it so if you figure it out before i do let me in on the secret!
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Next, rate... Do, Loop...

Posted 06 August 2007 - 08:03 AM

View PostTophCoder, on 6 Aug, 2007 - 07:57 AM, said:

AWWW ic, retard me. it should be the last thing before the loop. its the simple things i miss. but the code still is not working, im still messing with it so if you figure it out before i do let me in on the secret!


Put a messagebox after Do While number <= 1.0 I suspect it's only looping a single time (meaning your inner loop will only run a single time). I'm going to check into this, from the naked eye I see no reason why this isnt working
Was This Post Helpful? 0
  • +
  • -

#7 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: Next, rate... Do, Loop...

Posted 06 August 2007 - 08:07 AM

ok i put the messg box in. it is loopin down the 3yr to every rate% but it stops there, doesnt goto the 4yr term...

for my info, i put that mssg box in so i could see exactly what it was doing right? hehe
Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Next, rate... Do, Loop...

Posted 06 August 2007 - 08:36 AM

Here is the working code, I cant believe I didnt see it at first, in your inner loop you had Loop Until term <= 5. If it starts at 3 its always less than 5. But when I changed it to>= 5 it only looped until 4 (didnt display 5) so I changed it to >= 6 and it displays 3, 4, and 5

<edit>
Got rid of the >= 6 and changed it to > 5, this causes it to work properly :)</edit>
Private Sub xCalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCalcButton.Click
		'calculate monthly payments on a loan
		'using terms of 3, 4 and 5 years and interest
		'rates of 5% through 10%

		Const termheading As String = _
			"		   3 yrs	   4 yrs	   5 yrs"
		Dim principal As Double
		Dim monthlypayment As Double
		Dim isconverted As Boolean

		Me.xPaymentsLabel.Text = String.Empty
		isconverted = _
			Double.TryParse(Me.xPrincipalTextBox.Text, principal)

		If isconverted Then
			'display the term in the heading
			Me.xPaymentsLabel.Text = termheading _
				& ControlChars.NewLine
			'calculate and display payments
			Dim number As Double = 0.04
			Do While number <= 1.0#
				'number = number + 0.01 <----- You had it here
				Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text & number.ToString("P0") & "   "

				Dim term As Double = 3.0#

				Do
					'term = term + 1 <----- You had it here
					monthlypayment = -Financial.Pmt(number / 12.0#, term * 12.0#, principal)
					Me.xPaymentsLabel.Text += monthlypayment.ToString("N2") & " "

					Me.xPaymentsLabel.Text += ControlChars.NewLine
					term += 1 '<---- I moved it here, after all the calculations happened
				Loop Until term > 5.0#
				number = number + 0.01 '<---- I moved it here after all the work was done
			Loop

		Else	'principal cannot be converted to a number
			MessageBox.Show("Please re-enter the principal.", _
			"Payment Caluclator", MessageBoxButtons.OK, _
			MessageBoxIcon.Information)

		End If

		Me.xPrincipalTextBox.Focus()
		Me.xPrincipalTextBox.SelectAll()

	End Sub



Also, instead of label.text = label.text & you can use Label.Text += YourText in VB.Net. You confused me as you have this posted in the Visual Basic forum, not the VB.Net Forum

This post has been edited by PsychoCoder: 06 August 2007 - 08:43 AM

Was This Post Helpful? 0
  • +
  • -

#9 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: Next, rate... Do, Loop...

Posted 06 August 2007 - 08:43 AM

But im using visual basic express... i thought vb.net was a diff program language. im still really new
Was This Post Helpful? 0
  • +
  • -

#10 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Next, rate... Do, Loop...

Posted 06 August 2007 - 08:44 AM

View PostTophCoder, on 6 Aug, 2007 - 08:43 AM, said:

But im using visual basic express... i thought vb.net was a diff program language. im still really new


Nope, VB Express is VB.Net, its just the IDE has less features than Visual Studio, the language is the same. Also, I edited the above post, I changed >= 6 to > 5 to make it work right
Was This Post Helpful? 0
  • +
  • -

#11 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: Next, rate... Do, Loop...

Posted 06 August 2007 - 08:47 AM

OK i really feel like im bothering you with my coding problems... but i put the code in as a you said... its still not coming up right... would it be easier if i zipped the project and attached for you to see what im seeing?
Was This Post Helpful? 0
  • +
  • -

#12 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Next, rate... Do, Loop...

Posted 06 August 2007 - 08:51 AM

View PostTophCoder, on 6 Aug, 2007 - 08:47 AM, said:

OK i really feel like im bothering you with my coding problems... but i put the code in as a you said... its still not coming up right... would it be easier if i zipped the project and attached for you to see what im seeing?


You're not bothering me at all, so dont worry about it :)

Well this is weird, I created a form in VB.Net, added the label, textbox and button and got the right results. Albiet it showed up weird (3 yr on first line, then 4 and 5 on different lines below it) but it was displaying the right data).

If you want to zip it you can, but the code I put in my last post was straight from the form I created.
Was This Post Helpful? 0
  • +
  • -

#13 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: Next, rate... Do, Loop...

Posted 06 August 2007 - 08:54 AM

yeah it has 3yr then under the rates it has the 4yrs under5% and so on if you understand what im saying
Was This Post Helpful? 0
  • +
  • -

#14 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Next, rate... Do, Loop...

Posted 06 August 2007 - 09:03 AM

View PostTophCoder, on 6 Aug, 2007 - 08:54 AM, said:

yeah it has 3yr then under the rates it has the 4yrs under5% and so on if you understand what im saying


You need to fix how it's displaying but its putting out all 3 years worth of data, just isn't being displayed properly
Was This Post Helpful? 0
  • +
  • -

#15 TophCoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 209
  • Joined: 06-August 07

Re: Next, rate... Do, Loop...

Posted 06 August 2007 - 09:08 AM

hmmmmm i guess ill mess around with it alittle more...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2