Solved

variable problems involving val()

Posted on 2003-10-25
4
288 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…

778 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