Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,157 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,379 people online right now. Registration is fast and FREE... Join Now!




why is the output wrong on the calculation?

 
Reply to this topicStart new topic

why is the output wrong on the calculation?, two variables calculate and output wrong

cnewman
21 Jul, 2008 - 12:10 PM
Post #1

New D.I.C Head
*

Joined: 15 Sep, 2007
Posts: 14


My Contributions
Hi Community-

This place is awesome. Im really trying to get myself in to programming and always run into little walls that make me not want to push forward. I have spent the last few hours lookng at this code and can't figure why when I put these calculation is they output wrong.

What I am doing is simple. This is a basic Area and Perimeter calculator for triangles. Right now my formulas are entered wrong for simple troubleshooting. When I multiply 100 * 100 it gives me a return of 30,000 which is not what the answer is obviously. Does anyone perhaps see what I am doing wrong?

CODE

Public Class Form1
  
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLength.TextChanged

    End Sub

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

        Dim Width As Integer
        Dim Length As Integer
        Dim answerArea As Integer
        Dim answerPerimeter As Integer

        Width = CInt(txtWidth.Text)
        Length = CInt(txtLength.Text)

        answerArea = Width * Height
        answerPerimeter = Width + Length


        txtArea.Text = CStr(answerArea)
        txtPerimeter.Text = CStr(answerPerimeter)



    End Sub
End Class


Project is zipped and attached if needed.

Thank You kindly,
Christopher


Attached File(s)
Attached File  TriangleCalc.zip ( 63.96k ) Number of downloads: 8
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Why Is The Output Wrong On The Calculation?
21 Jul, 2008 - 12:23 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Moved to VB.NET
User is offlineProfile CardPM
+Quote Post

DeCompile
RE: Why Is The Output Wrong On The Calculation?
21 Jul, 2008 - 12:41 PM
Post #3

D.I.C Head
**

Joined: 20 Jul, 2008
Posts: 188



Thanked: 6 times
My Contributions
It sounds like you haven't declared your variables correctly.

Integers will only allow you to store any whole number eg. 1, 2, 3

Maybe you're using decimals..

For that you should be using ' float ' eg. 1.234

When you change your text, you don't need to convert to string. It's a waste of resources.

***********************

It could also be that your formula for calculations are wrong to.

Area of a Triange = (Base * Perpendicular Heigh) / 2

Perimeter of a Triance = Side + Side + Side

This post has been edited by DeCompile: 21 Jul, 2008 - 12:43 PM
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: Why Is The Output Wrong On The Calculation?
21 Jul, 2008 - 12:48 PM
Post #4

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 863



Thanked: 54 times
Dream Kudos: 2250
My Contributions
QUOTE(cnewman @ 21 Jul, 2008 - 09:10 PM) *

Hi Community-

This place is awesome. Im really trying to get myself in to programming and always run into little walls that make me not want to push forward. I have spent the last few hours lookng at this code and can't figure why when I put these calculation is they output wrong.

What I am doing is simple. This is a basic Area and Perimeter calculator for triangles. Right now my formulas are entered wrong for simple troubleshooting. When I multiply 100 * 100 it gives me a return of 30,000 which is not what the answer is obviously. Does anyone perhaps see what I am doing wrong?

CODE

Public Class Form1
  
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLength.TextChanged

    End Sub

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

        Dim Width As Integer
        Dim Length As Integer
        Dim answerArea As Integer
        Dim answerPerimeter As Integer

        Width = CInt(txtWidth.Text)
        Length = CInt(txtLength.Text)

        answerArea = Width * Height '<-Height of form
        answerPerimeter = Width + Length


        txtArea.Text = CStr(answerArea)
        txtPerimeter.Text = CStr(answerPerimeter)



    End Sub
End Class


Project is zipped and attached if needed.

Thank You kindly,
Christopher


Your using height not length, the value of the height will be the height of form (300).
Also perimeter answer is incorrect.

This post has been edited by AdamSpeight2008: 21 Jul, 2008 - 12:55 PM
User is offlineProfile CardPM
+Quote Post

WayneSpangler
RE: Why Is The Output Wrong On The Calculation?
21 Jul, 2008 - 12:52 PM
Post #5

D.I.C Head
**

Joined: 22 Mar, 2008
Posts: 103



Thanked: 9 times
My Contributions
For one thing:
CODE
answerArea = Width * Height

You are multipling your width with the form height. Height not being defined it defaults to Me.Height.
I try not to use properties of controls. Change it to read:
CODE
answerArea = Width * Length

As DeCompile said your formula are wrong. They look like a rectangle to me,
area of rectangle=length * width
parameter of rectangle=(length + Width) * 2



User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: Why Is The Output Wrong On The Calculation?
21 Jul, 2008 - 01:00 PM
Post #6

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 863



Thanked: 54 times
Dream Kudos: 2250
My Contributions
Area of Right-angle triangle=(width * height) /2
Perimeter = width + height + Sqr(width^2+length^2)
or if width=length
Perimeter = ~3.4142 * width

This post has been edited by AdamSpeight2008: 21 Jul, 2008 - 01:57 PM
User is offlineProfile CardPM
+Quote Post

cnewman
RE: Why Is The Output Wrong On The Calculation?
21 Jul, 2008 - 02:57 PM
Post #7

New D.I.C Head
*

Joined: 15 Sep, 2007
Posts: 14


My Contributions
icon_up.gif
Big thumbs up to all the help, I hope to pay it forward some day. The mistake on that variable was kind of embarrassing. The formula I used to just troubleshoot. Wow talk about stressing over something simple. I'm new to coding, and really appreciate the help.

THANK YOU ALL!
-Christopher
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:40AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month