Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

variable problems involving val()

Posted on 2003-10-25
4
Medium Priority
?
305 Views
Last Modified: 2010-05-03
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
Comment
Question by:neutrinohunter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 4

Assisted Solution

by:brother7
brother7 earned 80 total points
ID: 9619494
In Sub equals_Click(), you should move the line
   display.Caption = Result
to be the last line before End Sub.
0
 
LVL 39

Accepted Solution

by:
appari earned 80 total points
ID: 9619514
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
 
LVL 4

Expert Comment

by:brother7
ID: 9619543
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
 

Author Comment

by:neutrinohunter
ID: 9619631
dividing by 0 does not give the value 0 it gives the value of infinty
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question