# Bug Squashing My Calculator

Page 1 of 1

## 7 Replies - 915 Views - Last Post: 25 December 2012 - 06:37 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=304775&amp;s=b79f414e82628a28c234189db8274055&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Vodkacannon

Reputation: 0
• 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

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

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

• MrCupOfT

Reputation: 2298
• Posts: 9,535
• Joined: 29-May 08

## Re: Bug Squashing My Calculator

Posted 24 December 2012 - 10:49 PM

Tutorial on essential Debugging Skills

### #3 _HAWK_

• Master(Of Foo)

Reputation: 1162
• Posts: 4,444
• 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.

### #4 kai_itz me

Reputation: 28
• 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

### #5 trevster344

• The Peasant

Reputation: 225
• Posts: 1,526
• 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.

### #6 Vodkacannon

Reputation: 0
• 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

### #7 _HAWK_

• Master(Of Foo)

Reputation: 1162
• Posts: 4,444
• 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.

### #8 trevster344

• The Peasant

Reputation: 225
• Posts: 1,526
• 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.