10 Replies - 632 Views - Last Post: 08 November 2011 - 08:13 PM Rate Topic: -----

#1 Kakashi871  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-November 11

VB express 2008 Calculator

Posted 08 November 2011 - 04:13 AM

I've a homework on Calculator and this is what i got so far.
Only problem is when i type 1.2+0.2 it just don't come up right
and when i change the btn0 to the 1 similar to btn1 when at the start i could type 000000000 which shldnt be right on a calculator can someone help me debug this problem?
Public Class Form1
 
    'variables to hold operands
    Private Var1 As Double
    Private Var2 As Double
 
    'varible to hold temporary values
    Private Temp As Double
 
    'variable to hold operator type : +, -, *, /
    'used to detect whether the operator button is clicked for a few times
    Private [Operator] As String 
 
    'true when operator button click, false oterwise
    Private PendingOperator As Boolean
 
    'true when a dot is press else is false
    Private DotOperator As Boolean
 
    Public Sub Calculate()
        Var2 = CDbl(txt.Text)
        If [Operator] = "Add" Then
            Var1 = Var1 + Var2
        ElseIf [Operator] = "Subtract" Then
            Var1 = Var1 - Var2
        ElseIf [Operator] = "Multiply" Then
            Var1 = Var1 * Var2
        ElseIf [Operator] = "Divide" Then
            Var1 = Var1 / Var2
        End If
        txt.Text = CStr(Var1)
        PendingOperator = True
    End Sub
 
    'Btn 0-9 with exception of 0 all other button carries the same code
    Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
        If PendingOperator = False Then
            If txt.Text.Length > 0 Then
                txt.Text = txt.Text + CStr(0)
            End If
        End If
    End Sub
 
 
    Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
        If PendingOperator = False Then
            txt.Text = txt.Text + CStr(1)
        Else
            txt.Text = 1
            PendingOperator = False
        End If
    End Sub
 
 
    Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
        If PendingOperator = False Then
            txt.Text = txt.Text + CStr(2)
        Else
            txt.Text = 2
            PendingOperator = False
        End If
    End Sub
 
 
    Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
        If PendingOperator = False Then
            txt.Text = txt.Text + CStr(3)
        Else
            txt.Text = 3
            PendingOperator = False
        End If
    End Sub
 
 
    Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
        If PendingOperator = False Then
            txt.Text = txt.Text + CStr(4)
        Else
            txt.Text = 4
            PendingOperator = False
        End If
    End Sub
 
 
    Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
        If PendingOperator = False Then
            txt.Text = txt.Text + CStr(5)
        Else
            txt.Text = 5
            PendingOperator = False
        End If
    End Sub
 
 
    Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
        If PendingOperator = False Then
            txt.Text = txt.Text + CStr(6)
        Else
            txt.Text = 6
            PendingOperator = False
        End If
    End Sub
 
 
    Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
        If PendingOperator = False Then
            txt.Text = txt.Text + CStr(7)
        Else
            txt.Text = 7
            PendingOperator = False
        End If
    End Sub
 
 
    Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
        If PendingOperator = False Then
            txt.Text = txt.Text + CStr(8)
        Else
            txt.Text = 8
            PendingOperator = False
        End If
    End Sub
 
 
    Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
        If PendingOperator = False Then
            txt.Text = txt.Text + CStr(9)
        Else
            txt.Text = 9
            PendingOperator = False
        End If
    End Sub
 
   'cancel button
    Private Sub btnce_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnce.Click
        txt.Clear()
 
 
        Var1 = 0
        Var2 = 0
        [Operator] = ""
        DotOperator = False
 
 
    End Sub
 
 'percentage button
    Private Sub btnper_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnper.Click
        If txt.Text.Length <> 0 Then
            Temp = CDbl(txt.Text)
            Temp = (Temp / 100)
            txt.Text = CStr(Temp)
            DotOperator = False
        End If
    End Sub
 
 'squareroot button
    Private Sub btnsqrt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsqrt.Click
        If txt.Text.Length <> 0 Then
            Temp = CDbl(txt.Text)
            Temp = System.Math.Sqrt(Temp)
            txt.Text = CStr(Temp)
            DotOperator = False
        End If
    End Sub
 
 'btn +,-,x,/ all carries the same code
    Private Sub btndiv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndiv.Click
        If txt.Text.Length <> 0 Then
            If [Operator] = "" Then
                'first time clicking
                Var1 = CDbl(txt.Text)
                PendingOperator = True
            Else
                'click a few times
                Calculate()
            End If
            [Operator] = "Divide"
            DotOperator = False
 
 
        End If
    End Sub
 
 
    Private Sub btntime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntime.Click
        If txt.Text.Length <> 0 Then
            If [Operator] = "" Then
                'first time clicking x button
                Var1 = CDbl(txt.Text)
                PendingOperator = True
            Else
                'click a few times
                Calculate()
            End If
            [Operator] = "Multiply"
            DotOperator = False
 
 
        End If
    End Sub
 
 
    Private Sub btnmin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmin.Click
        If txt.Text.Length <> 0 Then
            If [Operator] = "" Then
                'first time clicking - button
                Var1 = CDbl(txt.Text)
                PendingOperator = True
            Else
                'click a few times
                Calculate()
            End If
            [Operator] = "Subtract"
            DotOperator = False
 
 
        End If
    End Sub
 
 
    Private Sub btnplu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnplu.Click
        If txt.Text.Length <> 0 Then
            If [Operator] = "" Then
                'first time clicking + button
                Var1 = CDbl(txt.Text)
                PendingOperator = True
            Else
                'click a few times
                Calculate()
            End If
            [Operator] = "Add"
            DotOperator = False
 
 
        End If
    End Sub
 
  'Decimal button
    Private Sub btndec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndec.Click
        If PendingOperator = False Then
            If DotOperator = False Then
                If txt.Text.Length > 0 Then
                    txt.Text = txt.Text + "."
                Else
                    txt.Text = "0."
                End If
                DotOperator = True
            End If
        End If
    End Sub
 
 'equal button
    Private Sub btneq_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneq.Click
        If txt.Text.Length <> 0 AndAlso Var1 <> 0 Then
            Calculate()
            [Operator] = ""
            DotOperator = False
        End If
    End Sub
End Class

This post has been edited by smohd: 08 November 2011 - 07:01 AM
Reason for edit:: Code tags added. Please use [code] tags when posting codes


Is This A Good Question/Topic? 0
  • +

Replies To: VB express 2008 Calculator

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1052
  • View blog
  • Posts: 4,077
  • Joined: 02-July 08

Re: VB express 2008 Calculator

Posted 08 November 2011 - 04:30 AM

Why on earth would you convert a number to string to do math with it! Please use code tags - select your code then hit the [CODE] button.

Math with string looks like this:

"1" + "1" = "11"

This post has been edited by _HAWK_: 08 November 2011 - 04:34 AM

Was This Post Helpful? 0
  • +
  • -

#3 Kakashi871  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-November 11

Re: VB express 2008 Calculator

Posted 08 November 2011 - 06:50 AM

View Post_HAWK_, on 08 November 2011 - 04:30 AM, said:

Why on earth would you convert a number to string to do math with it! Please use code tags - select your code then hit the [CODE] button.

Math with string looks like this:

"1" + "1" = "11"


It was given by the teacher and we are told to do small changes to it only

View Post_HAWK_, on 08 November 2011 - 04:30 AM, said:

Why on earth would you convert a number to string to do math with it! Please use code tags - select your code then hit the [CODE] button.

Math with string looks like this:

"1" + "1" = "11"


Anyway when it comes to +-*/ the variable will be converted to dbl due to the declaration of
Var1=CDbl(txt.Text)
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1052
  • View blog
  • Posts: 4,077
  • Joined: 02-July 08

Re: VB express 2008 Calculator

Posted 08 November 2011 - 07:03 AM

Quote

{inner monolog - escaping}If that code was from your teacher drop out, get you money back and find a higher quality class!!!


CDbl does convert a string to a double right up til it does not contain an appropriate string value to convert. I prefer to teach you Double.TryParse
Was This Post Helpful? 0
  • +
  • -

#5 Kakashi871  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-November 11

Re: VB express 2008 Calculator

Posted 08 November 2011 - 07:12 AM

View Post_HAWK_, on 08 November 2011 - 07:03 AM, said:

Quote

{inner monolog - escaping}If that code was from your teacher drop out, get you money back and find a higher quality class!!!


CDbl does convert a string to a double right up til it does not contain an appropriate string value to convert. I prefer to teach you Double.TryParse

it would help if i was allowed to do that -.-
Was This Post Helpful? 0
  • +
  • -

#6 trevster344  Icon User is offline

  • The Peasant
  • member icon

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

Re: VB express 2008 Calculator

Posted 08 November 2011 - 08:45 AM

Personally If I were you I'd tell that teacher that I'd rather learn something useful than fill my brain with crud such as horrible practices of programming.
Was This Post Helpful? 0
  • +
  • -

#7 Kakashi871  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-November 11

Re: VB express 2008 Calculator

Posted 08 November 2011 - 09:02 AM

View Posttrevster344, on 08 November 2011 - 08:45 AM, said:

Personally If I were you I'd tell that teacher that I'd rather learn something useful than fill my brain with crud such as horrible practices of programming.

which i cant do, just gotta solve this problem by hook or by crook or im gonna fail this assignment
Was This Post Helpful? 0
  • +
  • -

#8 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: VB express 2008 Calculator

Posted 08 November 2011 - 09:24 AM

So what you need to do at the beginning of the btn0 sub is check if the textbox starts with a 0 and contains a period. If it starts with a 0 and doesn't have a period, do nothing; if it starts with a 0 and has a period, or if it doesn't start with a 0, go ahead and add a 0.
If rtbReport.Text.StartsWith(CStr(0)) AndAlso rtbReport.Text.IndexOf(".") < 0 Then
    Exit Sub
Else
    If PendingOperator = False Then
        txt.Text &= CStr(0)
    Else
        txt.Text = 0
        PendingOperator = False
    End If
End If

This post has been edited by Tryparse: 08 November 2011 - 09:25 AM

Was This Post Helpful? 1
  • +
  • -

#9 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1052
  • View blog
  • Posts: 4,077
  • Joined: 02-July 08

Re: VB express 2008 Calculator

Posted 08 November 2011 - 09:47 AM

We have a Calculator tutorial here on DIC. It should give you plenty of ideas.
Was This Post Helpful? 0
  • +
  • -

#10 Kakashi871  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-November 11

Re: VB express 2008 Calculator

Posted 08 November 2011 - 07:20 PM

View PostTryparse, on 08 November 2011 - 09:24 AM, said:

So what you need to do at the beginning of the btn0 sub is check if the textbox starts with a 0 and contains a period. If it starts with a 0 and doesn't have a period, do nothing; if it starts with a 0 and has a period, or if it doesn't start with a 0, go ahead and add a 0.
If rtbReport.Text.StartsWith(CStr(0)) AndAlso rtbReport.Text.IndexOf(".") < 0 Then
    Exit Sub
Else
    If PendingOperator = False Then
        txt.Text &= CStr(0)
    Else
        txt.Text = 0
        PendingOperator = False
    End If
End If


Now I can't do 0.2+0.3
Was This Post Helpful? 0
  • +
  • -

#11 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: VB express 2008 Calculator

Posted 08 November 2011 - 08:13 PM

View PostKakashi871, on 08 November 2011 - 08:20 PM, said:

View PostTryparse, on 08 November 2011 - 09:24 AM, said:

So what you need to do at the beginning of the btn0 sub is check if the textbox starts with a 0 and contains a period. If it starts with a 0 and doesn't have a period, do nothing; if it starts with a 0 and has a period, or if it doesn't start with a 0, go ahead and add a 0.
If rtbReport.Text.StartsWith(CStr(0)) AndAlso rtbReport.Text.IndexOf(".") < 0 Then
    Exit Sub
Else
    If PendingOperator = False Then
        txt.Text &= CStr(0)
    Else
        txt.Text = 0
        PendingOperator = False
    End If
End If


Now I can't do 0.2+0.3

Hmm, that's odd ... I just did a quick & dirty copy & paste of your program with my code added in, and 0.2+0.3 worked fine for me. Are you sure you got everything in there? Here's what I have (only has addition functionality):
'variables to hold operands
    Private Var1 As Double
    Private Var2 As Double

    'varible to hold temporary values
    Private Temp As Double

    'variable to hold operator type : +, -, *, /
    'used to detect whether the operator button is clicked for a few times
    Private [Operator] As String

    'true when operator button click, false oterwise
    Private PendingOperator As Boolean

    'true when a dot is press else is false
    Private DotOperator As Boolean

    Public Sub Calculate()
        Var2 = CDbl(txt.Text)
        If [Operator] = "Add" Then
            Var1 = Var1 + Var2
        ElseIf [Operator] = "Subtract" Then
            Var1 = Var1 - Var2
        ElseIf [Operator] = "Multiply" Then
            Var1 = Var1 * Var2
        ElseIf [Operator] = "Divide" Then
            Var1 = Var1 / Var2
        End If
        txt.Text = CStr(Var1)
        PendingOperator = True
    End Sub

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

        If txt.Text.StartsWith(CStr(0)) AndAlso txt.Text.IndexOf(".") < 0 Then
            Exit Sub
        Else
            If PendingOperator = False Then
                txt.Text &= CStr(0)
            Else
                txt.Text = 0
                PendingOperator = False
            End If
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, _
    Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click

        Dim x As String = CType(sender, Button).Name.Substring(6)
        If PendingOperator = False Then
            txt.Text &= x
        Else
            txt.Text = x
            PendingOperator = False
        End If

    End Sub

    Private Sub btnDec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDec.Click

        If PendingOperator = False Then
            If DotOperator = False Then
                If txt.Text.Length > 0 Then
                    txt.Text = txt.Text + "."
                Else
                    txt.Text = "0."
                End If
                DotOperator = True
            End If
        End If

    End Sub

    Private Sub btnPls_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPls.Click

        If txt.Text.Length <> 0 Then
            If [Operator] = "" Then
                'first time clicking + button
                Var1 = CDbl(txt.Text)
                PendingOperator = True
            Else
                'click a few times
                Calculate()
            End If
            [Operator] = "Add"
            DotOperator = False
        End If

    End Sub

    Private Sub btnEq_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEq.Click

        If txt.Text.Length <> 0 AndAlso Var1 <> 0 Then
            Calculate()
            [Operator] = ""
            DotOperator = False
        End If

    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1