Do...While statement

calculating monthly payments with 3, 4, 5 term years & interest ra

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

41 Replies - 3489 Views - Last Post: 06 July 2008 - 08:26 PM Rate Topic: -----

#1 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Do...While statement

Post icon  Posted 04 July 2008 - 05:00 PM

Been working this one all day...So far the code works as it should until the Do Loop...Having trouble trying code it right...Here's what I have right now..


Option Explicit On
Option Strict On

Public Class MainForm

	Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
		Me.Close()
	End Sub

	Private Sub xPrincipalTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles xPrincipalTextBox.Enter
		' selects the existing text

		Me.xPrincipalTextBox.SelectAll()
	End Sub

	Private Sub xPrincipalTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles xPrincipalTextBox.TextChanged
		' clears the payments

		Me.xPaymentsLabel.Text = String.Empty
	End Sub

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

		Const term As String = _
		"	3yrs			  4yrs			  5yrs"
		Dim rate As String = "0.5, 0.6, 0.7, 0.8, 0.9, 1.0"
		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 = term _
			& ControlChars.NewLine

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

		End If

		Do While monthlyPayment = Financial.Pmt(rate / 12.0, _
			term * 12.0, principal)
			Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
			& monthlyPayment.ToString("N2") & " "

		Loop
	End Sub
End Class



I've tried writing the monthlyPayment, term, and rate separately also.

Is This A Good Question/Topic? 0
  • +

Replies To: Do...While statement

#2 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Do...While statement

Posted 04 July 2008 - 07:51 PM

Trying to change coarse, but getting errors on rate. Here's what I have right now:


Option Explicit On
Option Strict On

Public Class MainForm

	Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
		Me.Close()
	End Sub

	Private Sub xPrincipalTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles xPrincipalTextBox.Enter
		' selects the existing text

		Me.xPrincipalTextBox.SelectAll()
	End Sub

	Private Sub xPrincipalTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles xPrincipalTextBox.TextChanged
		' clears the payments

		Me.xPaymentsLabel.Text = String.Empty
	End Sub

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

		Const term As String = _
		"	3yrs			  4yrs			  5yrs"
		Dim rate As String = "0.5, 0.6, 0.7, 0.8, 0.9, 1.0"
		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 = term _
			& ControlChars.NewLine

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

		End If

		If CDbl(rate) = 0.05
			Me.xPaymentsLabel.Text = CStr(principal * CDbl(rate))

			
		End if
	End Sub
End Class


This post has been edited by LadyWolf: 04 July 2008 - 08:10 PM

Was This Post Helpful? 0
  • +
  • -

#3 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Do...While statement

Posted 04 July 2008 - 09:19 PM

mmmmmmmmmmmmmm starting over again :crazy:
Was This Post Helpful? 0
  • +
  • -

#4 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Do...While statement

Posted 04 July 2008 - 10:21 PM

mmmmmmmmmmm errors


Option Explicit On
Option Strict On

Public Class MainForm

	Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
		Me.Close()
	End Sub

	Private Sub xPrincipalTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles xPrincipalTextBox.Enter
		' selects the existing text

		Me.xPrincipalTextBox.SelectAll()
	End Sub

	Private Sub xPrincipalTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles xPrincipalTextBox.TextChanged
		' clears the payments

		Me.xPaymentsLabel.Text = String.Empty
	End Sub

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

		Const term As String = _
		"	3yrs			  4yrs			  5yrs"
		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 = term _
			& ControlChars.NewLine

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

		End If

		'calculate and display payments
	  Do error1--->Rate = "0.05, 0.06, 0.07, 0.08, 0.09, 0.1"
			Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
			&error2--->Rate.ToString("P0") & " "

  constant cannot be target---> term = 3.0
		monthlyPayment = _
		Financial.Pmt same as error2--->(Rate) / 12.0, _
		term * 12.0, principal)
			Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
			& monthlyPayment.ToString("N2") & " "


		Loop
	End Sub
End Class



Error 1 Syntax error. H:\ITCS1230\Chap06\Nested Payment Solution-Ex2\Nested Payment Solution\Nested Payment Project\Main Form.vb 56 10 Nested Payment Project


Error 2 Argument not specified for parameter 'NPer' of 'Public Function Rate(NPer As Double, Pmt As Double, PV As Double, [FV As Double = 0.0], [Due As Microsoft.VisualBasic.DueDate = Microsoft.VisualBasic.DueDate.EndOfPeriod], [Guess As Double = 0.1]) As Double'.\Nested Payment Solution\Nested Payment Project\Main Form.vb 58 15 Nested Payment Project
Was This Post Helpful? 0
  • +
  • -

#5 Damage  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 27
  • View blog
  • Posts: 1,079
  • Joined: 05-June 08

Re: Do...While statement

Posted 04 July 2008 - 11:23 PM

why'd you declare term as string? I'm assuming that term is what the user enters into the text box?

I know this is probably not what you want and i changed the math ,but i'm really hungry and not thinking straight right now but the code works and should hopefully help


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

		Dim term As Double
		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 = term.ToString


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

		End If

		term = CDbl(xPrincipalTextBox.Text)

		'calculate and display payments
		Dim rate As Double = 0.5
		Do While rate <= 1.0
			Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text & rate.ToString & " "

			monthlyPayment = Financial.Pmt((rate / 12.0), term * 12.0, principal)
			Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text & monthlyPayment.ToString & " "

			rate = rate + 0.1
		Loop
	End Sub




oh and i have no idea what the N2 in the tostring conversion is for, what did you want to achieve?

Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
& monthlyPayment.ToString("N2") & " "
Was This Post Helpful? 0
  • +
  • -

#6 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Do...While statement

Posted 05 July 2008 - 02:36 AM

I had to do a For...Next and a Do...Loop with the same outcome. The For..Next statement seemed easier to do for this, so I started with that one first. I was trying take what made the first one work and apply it to the Do..Loop. Here's the code for the For...Next, and what the XPaymentLabel should look like...


Option Explicit On
Option Strict On

Public Class MainForm

	Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
		Me.Close()
	End Sub

	Private Sub xPrincipalTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles xPrincipalTextBox.Enter
		' selects the existing text

		Me.xPrincipalTextBox.SelectAll()
	End Sub

	Private Sub xPrincipalTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles xPrincipalTextBox.TextChanged
		' clears the payments

		Me.xPaymentsLabel.Text = String.Empty
	End Sub

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

		Const TermHeading As String = _
				"		   3yrs		4yrs		5yrs"
		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
			For rate As Double = 0.05 To 0.1 Step 0.01
				Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
				& rate.ToString("P0") & " "

				For term As Double = 3.0 To 5.0
					monthlyPayment = _
					-Financial.Pmt(rate / 12.0, _
					term * 12.0, principal)
					Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
					& monthlyPayment.ToString("N2") & " "

				Next term

				Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text _
				& ControlChars.NewLine

			Next rate

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

		End If

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

	End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#7 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Do...While statement

Posted 05 July 2008 - 02:52 AM

Here's what it should look like...It'll answer your question why I was stringing "term"

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#8 Damage  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 27
  • View blog
  • Posts: 1,079
  • Joined: 05-June 08

Re: Do...While statement

Posted 05 July 2008 - 03:06 AM

ahh righty, gotcha
Was This Post Helpful? 0
  • +
  • -

#9 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Do...While statement

Posted 05 July 2008 - 03:26 AM

You had the right idea, without knowing how it should look. Like you I was so tired and not thinking straight...I probably should have posted the For...Next and screenshot earlier :blink:
Was This Post Helpful? 0
  • +
  • -

#10 Damage  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 27
  • View blog
  • Posts: 1,079
  • Joined: 05-June 08

Re: Do...While statement

Posted 05 July 2008 - 03:45 AM

:P it happens
Was This Post Helpful? 0
  • +
  • -

#11 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Do...While statement

Posted 05 July 2008 - 04:26 AM

Yep :D Still need help though, stringing "term" is causing error 'constant cannot be target of an assignment':crazy:
Was This Post Helpful? 0
  • +
  • -

#12 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Do...While statement

Posted 05 July 2008 - 10:24 AM

Working myself to death with this one :blink: Everything on up from 'calculate and display payments' works. Been trying to get the calculation to work...Getting error 'Argument 'NPer' is not a valid value'. Breakline shows that "term" has no value(0.0) I've worked string and double, but they come up with conversion errors. Here's the code I have at the moment:


Option Explicit On
Option Strict On

Public Class MainForm

	Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
		Me.Close()
	End Sub

	Private Sub xPrincipalTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles xPrincipalTextBox.Enter
		' selects the existing text

		Me.xPrincipalTextBox.SelectAll()
	End Sub

	Private Sub xPrincipalTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles xPrincipalTextBox.TextChanged
		' clears the payments

		Me.xPaymentsLabel.Text = String.Empty
	End Sub

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

		Const TermHeading As String = _
				"   3yrs			4yrs			5yrs"
		Dim rate As Double
		Dim term As Double
		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
			Rate = 0.5
			Do While rate >= 0.5
				Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text & rate.ToString & " "

(error line)---> monthlyPayment = Financial.Pmt((rate / 12.0), term * 12.0, principal)
				Me.xPaymentsLabel.Text = Me.xPaymentsLabel.Text & monthlyPayment.ToString & ""

				rate = rate + 0.01
			Loop

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

		End If


Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Do...While statement

Posted 05 July 2008 - 11:57 AM

Well done you've need done it, the problem function is missing one argument it the end.
Was This Post Helpful? 0
  • +
  • -

#14 LadyWolf  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 197
  • Joined: 25-June 08

Re: Do...While statement

Posted 05 July 2008 - 03:25 PM

View PostAdamSpeight2008, on 5 Jul, 2008 - 02:57 PM, said:

Well done you've need done it, the problem function is missing one argument it the end.


:unsure: Not sure what your trying to say 'the problem function is missing one argument it the end' Good to know I'm on the right track though :wub: Was starting to think I needed to start over again lol
Was This Post Helpful? 0
  • +
  • -

#15 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: Do...While statement

Posted 05 July 2008 - 03:59 PM

View PostLadyWolf, on 5 Jul, 2008 - 11:25 PM, said:

View PostAdamSpeight2008, on 5 Jul, 2008 - 02:57 PM, said:

Well done you've need done it, the problem function is missing one argument it the end.


:unsure: Not sure what your trying to say 'the problem function is missing one argument it the end' Good to know I'm on the right track though :wub: Was starting to think I needed to start over again lol

Sorry when I use that function I also use the two extra arguments.
The problem is caused by the NPer argument being zero.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3