Results 1 to 3 of 3

Thread: Troubles with my code

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2015
    Posts
    1

    Troubles with my code

    I am currently taking a course that is attempting to teach me coding however I have having a fair amount of trouble with it. I am stuck with one set of code that just doesn't seem to want to work.


    The assignment I am trying to complete is:
    Write a program to analyze a mortgage. I should enter the load, annual percentage rate of interest, and the duration of the loan in months.

    when the button is clicked, the monthly payment and the total amount of interest paid should be displayed. The formula for the monthly payment as given by the book is.

    payment = p*r/(1 -(1 +r)^(-n)) Where p is the amount of the loan, r is the monthly interest rate (Annual rate divided by 12) given as a number between 0 (for 0 percent) and 1 (for 100 percent), and n is the duration of the loan in months.

    My code is as follows:

    VB Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    4.         Dim rate As Double
    5.         Dim prin As Double
    6.         Dim term As Double
    7.         Dim thePay As Double
    8.         Dim totalInterest As Double
    9.  
    10.         If TextBoxPrin.Text = "" Then MsgBox("Enter Principle")
    11.         If TextBoxRate.Text = "" Then MsgBox("Enter Interest")
    12.         If TextBoxTerm.Text = "" Then MsgBox("Enter Term")
    13.  
    14.         If Not Double.TryParse(TextBoxPrin.Text, prin) Then
    15.             Exit Sub
    16.         End If
    17.  
    18.         If Not Double.TryParse(TextBoxInterest.Text, rate) Then
    19.             Exit Sub
    20.         End If
    21.  
    22.         If Not Double.TryParse(TextBoxTerm.Text, term) Then
    23.             Exit Sub
    24.         End If
    25.  
    26.         If rate >= 1 Then rate = rate / 100
    27.         rate = rate / 12
    28.         thePay = -((prin * rate) / (1 - ((1 + rate) ^ -term)))
    29.         totalInterest = ((term * thePay) - (-prin))
    30.  
    31.         TextBoxPayment.Text = thePay.ToString
    32.  
    33.         TextBoxInterest.Text = totalInterest.ToString
    34.     End Sub
    35. End Class
    When I enter the values for the principle ($18,000), rate(5.25%), and term (60 months) and press the button, nothing shows up in the payment textbox nor the interest textbox.

    I am stuck, any help would be greatly appreciated

    I am also new to the forums so if this is incorrectly posted, I am sorry.
    Last edited by FunkyDexter; Nov 9th, 2015 at 04:20 AM.

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,902

    Re: Troubles with my code

    I am also new to the forums so if this is incorrectly posted, I am sorry
    You got it pretty much right. The only thing you could have done better was was to use the highlight or code tags (see the VB and # buttons) to make your code a litle more reasonable. I've added them for you.

    I can't see any obvious reason why your code would fail but, if you're not seeing an error message, I would guess that one of your Double.TryParses is failing and an Exit Sub is being called. My suggestion would be to add some message boxes before the Exit Subs.

    Other than that, have you worked out how to debug your code and walk through it yet? If so, put a break point at the start of the function then step through the code a line at a time and see what happens.

    Welcome to the forum.


    edit> Hang on, when you say: "I enter the values for the principle ($18,000), rate(5.25%), and term (60 months)" do you mean you actualy type in the $, % and months characters/words? If so then that's your problem. $60 can't be parsed to a double. 60 can. You should either type only the numbers or you should add code to extract the numbers from the user input
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Troubles with my code

    Code like this is how you make nothing happen when something goes wrong:
    Code:
    If Not Double.TryParse(TextBoxPrin.Text, prin) Then
        Exit Sub
    End If
    But the problem is, when you do that, nothing happens when something goes wrong. If there's a problem with your input, you don't get any indicator other than you don't get the results you should expect.

    At least when debugging, put some Console.WriteLine() or MessageBox.Show() calls that tell you about what's going on:
    Code:
    If Not Double.TryParse(TextBoxPrin.Text, prin) Then
        MessageBox.Show("Whoa, something's wrong with Prin!")
    End If
    I'm pretty sure FunkyDexter is right about the problem. Computers are stuffy, and if you ask them to tell you what number "$25,000" is they'll say "$ is not a number". You have to go out of your way to deal with dollar signs and digit separators. It's not very far out of your way, but if you're just getting started you can probably just live with "don't type those things".
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width