Solved

# variable problems involving val()

Posted on 2003-10-25
294 Views
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
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
• 2

LVL 4

Assisted Solution

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

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

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

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

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

Best way to parse out a json string in VB6? 10 396
vb6 connector to mongodb 2 163
Exit a vb6 apps when a calling it apps closes 15 63
Problem to With line 4 87
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
###### Suggested Courses
Course of the Month3 days, 13 hours left to enroll