Results 1 to 12 of 12

Thread: Hello, quick code question that is likely very noobish

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    3

    Hello, quick code question that is likely very noobish

    Hi, first sorry if this is in the wrong forum - it seemed fairly right so I'm taking a shot.

    I'm in college and just starting my first programming class, we're only using VB6 and mostly just working up some really basic GUIs. I'm trying to work ahead in my class because I know I'll get stuck really bad sooner or later and I want to have a lot of time to work out the kinks. First few projects went okay and I was able to work out my problems. Now I'm just stuck and I can't find a reason WHY I am stuck.

    It's just a little GUI to work out a mortgage payment, I've gotten 95% of it to work just fine but I can't seem to get it to do one equation right. For the life of me I can't find WHY it isn't working.

    Here is the code I have so far:
    Code:
    Private Sub btnAnalyze_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnalyze.Click
            Dim p As Integer
            Dim r As Decimal
            Dim n As Integer
            If IsNumeric(txtAmountofloan.Text) Then
                p = (txtAmountofloan.Text)
            Else
                MessageBox.Show("ERROR! Please fill in the loan ammount")
                Return
            End If
            If IsNumeric(txtInterest_rate.Text) Then
                r = (txtInterest_rate.Text)
            Else
                MessageBox.Show("ERROR! Please fill in the interest rate")
                Return
            End If
            If IsNumeric(txtMonths.Text) Then
                n = (txtMonths.Text)
            Else
                MessageBox.Show("ERROR! Please fill in the duration of the loan in months")
                Return
            End If
            Dim monthly_payment As Double
            monthly_payment = (p * r / 1 - (1 + r ^ (-n)))
            Dim total_interest_paid As Integer = (r * monthly_payment - p)
            txtPayment.Text = monthly_payment.ToString("C")
            txtInterest_paid.Text = (total_interest_paid.ToString("C"))
    
        End Sub

    This is version about 15 or 20 of basically the same code so it might look a little weird as for the past ten passes I've just been changing random things to see if I can get it to work right.

    The problem (I think) is coming from
    Code:
    monthly_payment = (p * r / 1 - (1 + r ^ (-n)))
    Everything works fine until it gets here, it isn't dividing. At all. It's just doing the P*R part and then subtracting 1. This is the equation that I was given by the text book and I've checked the chapter three times and it doesn't say there should be anything special about it.

    I'v tried saving my P, R, and N variables as double, string, integer, and decimal to no effect. I've tried the
    Code:
    monthly_payment = (p * r / 1 - (1 + r ^ (-n)))
    equation as 7 or 8 different types, such as
    Code:
    monthly_payment = p * r / 1 - (1 + r ^ (-n))
    Code:
    monthly_payment = p * r / 1 - (1 + r) ^ (-n)
    Code:
    monthly_payment = (p * r / 1 - (1 + r) ^ (-n))
    etc. No matter what I change I keep getting one of three answers when I run the program and put in the values, infinity, 45,700, or 45,699. All of these are wrong.


    In case it helps, this is part of the assignment itself where it talks about the equation:

    The formula for the monthly payment 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. The formula for the total interest paid is:


    Any help would be awesome, I'm sure there is something I'm missing and it's likely really dumb of me to miss but I just can't find it.

    Thanks

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Hello, quick code question that is likely very noobish

    Well it is definitely dividing but your formula is incorrect and it is dividing by 1 so it may appear that it did not divide at all but it did
    You have
    Code:
    monthly_payment = (p * r / 1 - (1 + r ^ (-n)))
    where the book has
    Code:
    Payment = p * r / (1 – (1 + r) ^ (-n)),

    note the placement of the ) before the ^ in the example
    Also note the ( after the / in the example which is not present in your code

    placement of ( )s can make a huge difference in an equation without them any multiplication and division are done in order left to right and then addition and subtraction
    Last edited by DataMiser; Aug 19th, 2014 at 01:07 PM.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Hello, quick code question that is likely very noobish

    Clearly no VB6 here, this is all VB.Net of some vintage or other.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Hello, quick code question that is likely very noobish

    Quote Originally Posted by dilettante View Post
    Clearly no VB6 here, this is all VB.Net of some vintage or other.
    Yep, I did not even notice that I just scanned down the post to see what problem he was having and saw that the formula was wrong.
    Clearly VB.Net based on the arguments of the button click sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    3

    Re: Hello, quick code question that is likely very noobish

    Quote Originally Posted by DataMiser View Post
    Well it is definitely dividing but your formula is incorrect and it is dividing by 1 so it may appear that it did not divide at all but it did
    You have
    Code:
    monthly_payment = (p * r / 1 - (1 + r ^ (-n)))
    where the book has
    Code:
    Payment = p * r / (1 – (1 + r) ^ (-n)),

    note the placement of the ) before the ^ in the example
    Also note the ( after the / in the example which is not present in your code

    placement of ( )s can make a huge difference in an equation without them any multiplication and division are done in order left to right and then addition and subtraction



    Nope, tried that already. It was how I did it the first time.

    Put it in again just now to double check

    Code:
    p * r / (1 – (1 + r) ^ (-n))
    returns $47,500.

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2014
    Posts
    3

    Re: Hello, quick code question that is likely very noobish

    Quote Originally Posted by dilettante View Post
    Clearly no VB6 here, this is all VB.Net of some vintage or other.

    sorry, VB 2010 express. The text book assigned with the course was calling it VB6, maybe I got the names wrong.

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Hello, quick code question that is likely very noobish

    Quote Originally Posted by Lockybalboa View Post
    Nope, tried that already. It was how I did it the first time.


    returns $47,500.
    Well you have not told us what values you are entering so the 47.500 means nothing to us.

    I tested it with the formula from the book 100,000 loan, .05 interest and 12 months and the payments came out to $11,282.54 per month but the interest paid was way off at ($99,436.00)
    sorry, VB 2010 express. The text book assigned with the course was calling it VB6, maybe I got the names wrong.
    Definitely not VB6 Vb6 Does not have a MessageBox.Show it instead has Msgbox so if your book is telling you to use things like MessageBox.show it is intended for VB.Net if not then perhaps it is intended for VB6 but that is not what you are using VB 2010 is VB10 VB6 is VB 1998
    Last edited by DataMiser; Aug 19th, 2014 at 03:16 PM.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Hello, quick code question that is likely very noobish

    Thread moved from the 'VB6 and Earlier' (aka VB 1998 and earlier) forum to the 'VB.Net' (VB2002 and later) forum

  9. #9
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,910

    Re: Hello, quick code question that is likely very noobish

    -- What is the Return command being inside your source code that the OP had posted in the very first post. Since that I thought that was removed when Visual Basic went to .NET. Didn't that work from GoSub commands, but was phased out when they went to .NET???
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Hello, quick code question that is likely very noobish

    The GoSub based Return was phased out about 20 years ago, along with GoSub.

    The .Net version of the Return statement is the equivalent of these:
    Code:
      Exit Sub
    Code:
      Exit Function
    ... and if there is a value (eg: Return myValue), it is the equivalent of this in VB6 and earlier:
    Code:
      FunctionName = myValue
      Exit Function

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Hello, quick code question that is likely very noobish

    If you still have the problem, we need to see your inputs, outputs and the code you're now using.
    It'd be helpful if you can tell us what you expect the outputs to be...

  12. #12
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Hello, quick code question that is likely very noobish

    Quote Originally Posted by ThEiMp View Post
    -- What is the Return command being inside your source code that the OP had posted in the very first post. Since that I thought that was removed when Visual Basic went to .NET. Didn't that work from GoSub commands, but was phased out when they went to .NET???
    Really?

    When you create a function in VB.Net you return a value using the Return statement
    Code:
    Private Function SomeFunction() As String
         'do some stuff
         Return "Hi"
    End Function

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