7 Replies - 525 Views - Last Post: 25 December 2012 - 06:37 PM Rate Topic: -----

#1 Vodkacannon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 07-November 11

Bug Squashing My Calculator

Posted 24 December 2012 - 10:21 PM

I wanted to test if my calculator would work so I did a basic test with division and I seem to always get a result as 0 when I divide two #'s.
Line 106 starts the division code and line 140 may also be of interest.

I used a modulus because in the future I want the program to be able to allow the user to do continued operations while temporarily storing numbers in two different variables. The variable that the number is stored in is determined by if the loop index # is divisible by 2 or not.

Public Class Form1

    Dim index As Integer = 0

    Dim stream1, stream2 As Double

    Dim add As Boolean = False, subtract As Boolean = False, multiply As Boolean = False,
        division As Boolean = False, power As Boolean = False, root As Boolean = False

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        txtStreamBox.Text += "1"

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        txtStreamBox.Text += "2"

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        txtStreamBox.Text += "3"

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        txtStreamBox.Text += "4"

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

        txtStreamBox.Text += "5"

    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

        txtStreamBox.Text += "6"

    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click

        txtStreamBox.Text += "7"

    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

        txtStreamBox.Text += "8"

    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click

        txtStreamBox.Text += "9"

    End Sub

    Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles period.Click

        txtStreamBox.Text += "."

    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button0.Click

        txtStreamBox.Text += "0"

    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles plus.Click

        add = True

        stream1 = Val(txtStreamBox.Text)

        txtStreamBox.Text = ""

    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles minus.Click

        subtract = True

        stream1 = Val(txtStreamBox.Text)

        txtStreamBox.Text = ""

    End Sub

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles times.Click

        multiply = True

        stream2 = Val(txtStreamBox.Text)

        txtStreamBox.Text = ""

    End Sub

    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles divide.Click

        division = True

        index = index + 1

        If index Mod 2 = 0 Then

            stream1 = Val(txtStreamBox.Text)

            txtStreamBox.Text = ""
        Else
            stream2 = Val(txtStreamBox.Text)

            txtStreamBox.Text = ""

        End If

    End Sub

    Private Sub compute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles compute.Click

        If add = True Then

            txtStreamBox.Text = stream1 + stream2

        ElseIf subtract = True Then

            txtStreamBox.Text = stream1 - stream2

        ElseIf multiply = True Then

            txtStreamBox.Text = stream1 * stream2

        ElseIf division = True Then

            txtStreamBox.Text = stream1 / stream2

        ElseIf power = True Then

            txtStreamBox.Text = Math.Pow(stream1, stream2)

        End If

    End Sub

End Class




Is This A Good Question/Topic? 0
  • +

Replies To: Bug Squashing My Calculator

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: Bug Squashing My Calculator

Posted 24 December 2012 - 10:49 PM

Tutorial on essential Debugging Skills
Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,174
  • Joined: 02-July 08

Re: Bug Squashing My Calculator

Posted 24 December 2012 - 10:57 PM

Option Strict On would be helpful to your learning. It would force you to convert your data types. Doing math with a string = bad code.
Was This Post Helpful? 1
  • +
  • -

#4 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: Bug Squashing My Calculator

Posted 25 December 2012 - 01:00 AM

EVERY THING IS PERFECT ..even you dont have to mention (dim index as integer = 0 and index = index + 1),
bydefault it is 0.

just CHANGE YOUR LINE 112 and 142 WITH the below code...
 If (stream1 Mod 2) = 0 Then 
 


txtStreamBox.Text = stream1 / txtStreamBox.Text




becoz ur code only taking value from stream1
after that empty ur string again it put the value of stream1

This post has been edited by kai_itz me: 25 December 2012 - 02:30 AM

Was This Post Helpful? 1
  • +
  • -

#5 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,511
  • Joined: 16-March 11

Re: Bug Squashing My Calculator

Posted 25 December 2012 - 12:02 PM

I seriously urge you not to do math using strings as _Hawk said. It's terrible terrible practice and you're going to send yourself on a wild goose chase despite building simple programs.
Was This Post Helpful? 0
  • +
  • -

#6 Vodkacannon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 07-November 11

Re: Bug Squashing My Calculator

Posted 25 December 2012 - 04:16 PM

Appreciated. Thanks everyone.

I will store the strings in double variables first, then do math ops. on them.

The step by step debugging is also useful especially when you have long code and down want to rip your hair out.

Do you know how the prepare you these days at school?
They Don't!
Our programming teacher doesn't even have a degree in computer science, not even a minor. My teacher is not qualified at all. I know more than my teacher!
We actually use Val(etc.Text) and do math ops. with it directly.
Students are not being taught the right way. It's not rigorous enough.

This post has been edited by Vodkacannon: 25 December 2012 - 04:23 PM

Was This Post Helpful? 0
  • +
  • -

#7 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,174
  • Joined: 02-July 08

Re: Bug Squashing My Calculator

Posted 25 December 2012 - 06:33 PM

Lots of us here are self taught. Just get involved with a community we will keep ya straight. Once you learn how to research you can learn anything.
Was This Post Helpful? 1
  • +
  • -

#8 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,511
  • Joined: 16-March 11

Re: Bug Squashing My Calculator

Posted 25 December 2012 - 06:37 PM

Almost two years ago I became involved with the D.I.C. community and I've learned more since then than I did the 6 years before that. Practice practice.. each week you put a certain amount of time into learning something new and you'll be fluent in no time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1