Solved

variable problems involving val()

Posted on 2003-10-25
4
290 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
  • 2
4 Comments
 
LVL 4

Assisted Solution

by:brother7
brother7 earned 20 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 20 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 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