Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

variable problems involving val()

I have tried to do a calculator of an example somebody showed me but their is somehing wrong, it might be what i have typed but ive checked it and it seems fine.

the problem is with the variables i have declared operand, operator1 and operand2.

when i press the equals button is is not adding(if add as pressed) the variables,

its not working for any of the operators i have at the moment either;

ill post the code, can any one help?

Option Explicit
Dim Operand1 As Double, Operand2 As Double
Dim Operator As String
Dim cleardisplay As Boolean

Private Sub Clearbttn_Click()
display.Caption = ""
End Sub

Private Sub Digits_Click(Index As Integer)
If cleardisplay Then
display.Caption = ""
cleardisplay = False
End If
display.Caption = display.Caption + Digits(Index).Caption
End Sub

Private Sub divide_Click()
Operand1 = Val(display.Caption)
Operator = "/"
display.Caption = ""
End Sub

Private Sub Dobbtn_Click()
If InStr(display.Caption, ".") Then
Exit Sub
Else
display.Caption = display.Caption + "."
End If
End Sub

Private Sub equals_Click()
Dim Result As Double
Operand2 = Val(display.Caption)
If Operator = "+" Then
Result = (Operand1) + (Operand2)
If Operator = "*" Then
Result = (Operand1 * Operand2)
If Operator = "-" Then
Result = (Operand1 - Operand2)
If Operator = "/" And Operand2 <> "0" Then
    Result = (Operand1 / Operand2)
    display.Caption = Result
End If
End If
End If
End If
End Sub

Private Sub minus_Click()
Operand1 = Val(display.Caption)
Operator = "-"
display.Caption = ""

End Sub

Private Sub plus_Click()
Operand1 = Val(display.Caption)
Operator = "+"
display.Caption = ""
End Sub

Private Sub times_Click()
Operand1 = Val(display.Caption)
Operator = "*"
display.Caption = ""
End Sub
0
neutrinohunter
Asked:
neutrinohunter
  • 2
2 Solutions
 
brother7Commented:
In Sub equals_Click(), you should move the line
   display.Caption = Result
to be the last line before End Sub.
0
 
appariCommented:
try this

Private Sub equals_Click()
Dim Result As Double
Operand2 = Val(display.Caption)
Select Case Operator
Case "+"
    Result = (Operand1) + (Operand2)
Case "-"
    Result = (Operand1 - Operand2)
Case "*"
    Result = (Operand1 * Operand2)
Case "/"
    Result = (Operand1 / Operand2)
End Select

    display.Caption = Result
End Sub
0
 
brother7Commented:
Also, the logic of your nested If statements is wrong.  You need to have some Else statements added.  The Sub equals_Click() routine should read something like this:

--- Code Start ---

      Dim Result As Double
      operand2 = Val(display.Caption)
      If operator = "+" Then
         Result = (operand1) + (operand2)
      Else
         If operator = "*" Then
            Result = (operand1 * operand2)
         Else
            If operator = "-" Then
               Result = (operand1 - operand2)
            Else
               If operator = "/" And operand2 <> 0 Then
                  Result = (operand1 / operand2)
               End If
            End If
         End If
      End If
      display.Caption = Result

--- Code End ---

Also note... as you have the routine written, if you attempt to divide by 0, then the Result will be 0 (the default value for a Double variable).
0
 
neutrinohunterAuthor Commented:
dividing by 0 does not give the value 0 it gives the value of infinty
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now