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

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




And yet another Calculator problem

 
Reply to this topicStart new topic

And yet another Calculator problem, But this time i'm using arrays please give suggestions

elstupido
19 Jul, 2008 - 07:07 AM
Post #1

New D.I.C Head
*

Joined: 19 Jul, 2008
Posts: 3

CODE
' note: only can hold up to 15-17 numbers
Private num1, num2, Result, Index, opt, optn As Double
Dim dude As Boolean

Private Sub C_Click()
num1 = 0
num2 = 0
Value.Text = ""
End Sub

Private Sub CE_Click()
num2 = 0
Value.Text = ""
End Sub

Private Sub Deci_Click()
Value.Text = Val(Value.Text) & "."
End Sub

Private Sub Equals_Click()
    dude = 1
    num2 = Val(Value.Text)
    If opt = 0 Then
        Result = num1 + num2
    ElseIf opt = 1 Then
        Result = num1 - num2
    ElseIf opt = 2 Then
        Result = num1 * num2
    ElseIf opt = 3 Then
        If num2 = 0 Then
        Value.Text = ""
        MsgBox "Answer is undefined", 0, "Error"
        Else
        Result = num1 / num2
        End If
    End If
    Value.Text = Result
    num1 = 0
    num2 = 0
End Sub

Private Sub negpos_Click()
Result = 0 - Value.Text
Value.Text = Result
End Sub

Private Sub Number_Click(Index As Integer)
    If dude = True Then
        Value.Text = ""
        If Value.Text = "" Then
            dude = False
        End If
    End If
    Value.Text = Val(Value.Text & Number(Index).Index)
End Sub

Private Sub Operator_Click(Index As Integer)
    opt = Operator(Index).Index
    num1 = Val(Value.Text)
    Value.Text = ""
End Sub

Private Sub Percent_Click()
    Result = Value.Text / 100
    Value.Text = Result
End Sub


The problems to this:
1.) decimal gets all funky and stuff (in other words once you press the decimal command button and hit the zero command button, it automatically switches back to zero instead of a "0.0")
2) it can only display up to 15 numbers
3) I have no idea if i got my code right... it's kinda an experimental calculator
User is offlineProfile CardPM
+Quote Post

elstupido
RE: And Yet Another Calculator Problem
19 Jul, 2008 - 09:24 AM
Post #2

New D.I.C Head
*

Joined: 19 Jul, 2008
Posts: 3

yea I kinda figured out how to work the decimal problem out and i straightened and neatened my code a bit....
but another problem arose...

i can't seem to subtract or divide continously....

it still displays up to 15 numbers

CODE

Dim num1 As Double
Dim num2 As Double
Dim result As Double
Dim opt As Integer
Dim dic As Boolean

Private Sub cmdNUM_Click(Index As Integer)
    If dic = True Then
        txtnum = ""
        If txtnum = "" Then
            dic = False
        End If
    End If
    txtnum = txtnum & cmdnum(Index).Index
End Sub

Private Sub cmdEQUALS_Click()
    dic = True
    num2 = Val(txtnum)
    Select Case opt
        Case Is = 0
            result = num1 + num2
        Case Is = 1
            result = num1 - num2
        Case Is = 2
            result = num1 * num2
        Case Is = 3
            If num2 = 0 Then
                MsgBox "Answer is Undefined", 0, "Error"
            Else
                result = num1 / num2
            End If
    End Select
    txtnum = result
End Sub

Private Sub cmdDECI_Click()
    txtnum = txtnum + "."
End Sub

Private Sub cmdOPRTR_Click(Index As Integer)
    opt = cmdoprtr(Index).Index
    num1 = Val(txtnum)
    txtnum = ""
End Sub

Private Sub cmdSIGN_Click()
    txtnum = 0 - txtnum
End Sub

Private Sub cmdEXIT_Click()
    End
End Sub

End Sub


To Explain this further my settings include 1 textbox, 18 command buttons (10 are in the array cmdNUM, 4 are in the array cmdOPRTR)

This post has been edited by elstupido: 19 Jul, 2008 - 09:35 AM
User is offlineProfile CardPM
+Quote Post

woodjom
RE: And Yet Another Calculator Problem
19 Jul, 2008 - 08:56 PM
Post #3

New D.I.C Head
*

Joined: 8 May, 2008
Posts: 46



Thanked: 1 times
My Contributions
have you tried using the CDBL and CSTR conversion functions? Check those features out instead of forcing the cast try using the core functions available to do what you need. Also, check out the precision of the data type as well. I cant remember but the DOUBLE data type is probably the largest precision you can get for decimal but have you tried to making the variable an Unsigned Double, which will effectively double your length of precision by cutting out the negative side of the range. Not sure if you need the negative side....as well you could probably take the number apart and do the math in parts and then cojoin them together as a string.

of course i dont know what kind of precision you need but check the data types for VB6, which is what it looks like you are using.
User is offlineProfile CardPM
+Quote Post

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

Be Social

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

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month