• Status: Solved
• Priority: Medium
• Security: Public
• Views: 169

# Help! Simple Calculator project bug when dividing by zero

I need the below statement to give me a "Divide Zero" display when an number is divided by zero, but in this case I am getting the "divide zero" display when clicking Zero then the "divide" button.  Below is the section of my code for dividing, actually if I only click the / (i.e. divide button) i get the "divide zero" as well, which I don't want. Help!

Private Sub math_Click(Index As Integer)
total2 = total1
total1 = 0
test = math(Index).Caption

If testlast = "1" Or testlast = "=" Then

testlast = test

If test = "/" Then
If total1 = 0 And total2 = 0 And gtotal = 0 Then
outPut.Caption = "Divide Zero"
testlast = test
Else
If total1 = 0 And gtotal = 0 Then
total1 = 1
gtotal = 1
gtotal = (total2 / total1) / gtotal 'special because first instance
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 And total1 = 0 Then
total1 = 1
total2 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total1 = 0 Then
total1 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 Then
total2 = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If gtotal = 0 Then
gtotal = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
End If
End If
End If
End If
End If
End If
0
GlobalFax
• 14
• 12
• 4
• +1
1 Solution

Commented:
lets see a bit more code.....i'm sure it's a simple fix
0

Commented:
total1, 2 and 3 are all equal to zero if your code executes that line.  Where are they defined, and what are they used for?
0

Author Commented:
Option Explicit
Dim flag As Integer
Dim total1 As Double
Dim total1a As Double
Dim total2 As Double
Dim total2a As Double
Dim gtotal As Double
Dim gtotala As Double
Dim test As String
Dim testlast As String

Private Sub calcBut_Click(Index As Integer)
If total2 = 0 And total1 = 0 And gtotal = 0 Then
outPut.Caption = outPut.Caption & calcBut(Index).Caption
total1 = outPut.Caption
Else
If total1 = 0 Then
outPut.Caption = ""
outPut.Caption = outPut.Caption & calcBut(Index).Caption
total1 = outPut.Caption
Else
outPut.Caption = outPut.Caption & calcBut(Index).Caption
total1 = outPut.Caption
End If
End If
End Sub

Private Sub clear_Click()
outPut.Caption = ""
total1 = 0
gtotal = 0
testlast = "1"
End Sub

Private Sub cmdExit_Click()
End Sub

Private Sub Command1_Click()
outPut.Caption = gtotal
total1 = 0
total2 = 0
End Sub

Private Sub Command2_Click()
If outPut.Caption = "" Then
outPut.Caption = ""
Else
outPut.Caption = -(outPut.Caption)
gtotal = gtotal * -1

End If

End Sub

Top = (Screen.Height - neg.Height) / 2
Left = (Screen.Width - neg.Width) / 2
gtotal = 0
testlast = "1"
End Sub

Private Sub math_Click(Index As Integer)
total2 = total1
total1 = 0
test = math(Index).Caption

If testlast = "1" Or testlast = "=" Then

testlast = test

If test = "/" Then
If total1 = 0 And total2 = 0 And gtotal = 0 Then
outPut.Caption = "Divide Zero"
testlast = test
Else
If total1 = 0 And gtotal = 0 Then
total1 = 1
gtotal = 1
gtotal = (total2 / total1) / gtotal 'special because first instance
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 And total1 = 0 Then
total1 = 1
total2 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total1 = 0 Then
total1 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 Then
total2 = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If gtotal = 0 Then
gtotal = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
End If
End If
End If
End If
End If
End If
0

Commented:
what are total1, 2 and 3 for?
0

Commented:
please post the source from the .frm file (include the souce that creates the buttons, labels etc...)
0

Commented:
please post the source from the .frm file (include the souce that creates the buttons, labels etc...)
0

Author Commented:
Here's the entire Enchilada, Jacamar....I ask myself that same question.  Hope the entire picture helps you out.

Option Explicit
Dim flag As Integer
Dim total1 As Double
Dim total1a As Double
Dim total2 As Double
Dim total2a As Double
Dim gtotal As Double
Dim gtotala As Double
Dim test As String
Dim testlast As String

Private Sub calcBut_Click(Index As Integer)
If total2 = 0 And total1 = 0 And gtotal = 0 Then
outPut.Caption = outPut.Caption & calcBut(Index).Caption
total1 = outPut.Caption
Else
If total1 = 0 Then
outPut.Caption = ""
outPut.Caption = outPut.Caption & calcBut(Index).Caption
total1 = outPut.Caption
Else
outPut.Caption = outPut.Caption & calcBut(Index).Caption
total1 = outPut.Caption
End If
End If
End Sub

Private Sub clear_Click()
outPut.Caption = ""
total1 = 0
gtotal = 0
testlast = "1"
End Sub

Private Sub cmdExit_Click()
End Sub

Private Sub Command1_Click()
outPut.Caption = gtotal
total1 = 0
total2 = 0
End Sub

Private Sub Command2_Click()
If outPut.Caption = "" Then
outPut.Caption = ""
Else
outPut.Caption = -(outPut.Caption)
gtotal = gtotal * -1

End If

End Sub

Top = (Screen.Height - neg.Height) / 2
Left = (Screen.Width - neg.Width) / 2
gtotal = 0
testlast = "1"
End Sub

Private Sub math_Click(Index As Integer)
total2 = total1
total1 = 0
test = math(Index).Caption

If testlast = "1" Or testlast = "=" Then

testlast = test

If test = "/" Then
If total1 = 0 And total2 = 0 And gtotal = 0 Then
outPut.Caption = "Divide Zero"
testlast = test
Else
If total1 = 0 And gtotal = 0 Then
total1 = 1
gtotal = 1
gtotal = (total2 / total1) / gtotal 'special because first instance
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 And total1 = 0 Then
total1 = 1
total2 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total1 = 0 Then
total1 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 Then
total2 = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If gtotal = 0 Then
gtotal = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
End If
End If
End If
End If
End If
End If

ElseIf test = "*" Then
If total1 = 0 And total2 = 0 Then
total2 = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
test = testlast
Else
If gtotal = 0 Then
gtotal = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
test = testlast
Else
If total2 = 0 Then
total2 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total1 = 0 Then
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
End If
End If
End If
End If

ElseIf test = "+" Then
gtotal = gtotal + total1 + total2
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test

ElseIf test = "-" Then
If gtotal = 0 Then
gtotal = total2 - gtotal
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
Else
gtotal = gtotal - total2
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
End If
End If
Else

If testlast = "*" Then
If total1 = 0 And total2 = 0 Then
total2 = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If gtotal = 0 Then
gtotal = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 Then
total2 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total1 = 0 Then
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
End If
End If
End If
End If

ElseIf testlast = "+" Then
gtotal = gtotal + total1 + total2
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test

ElseIf testlast = "-" Then
If gtotal = 0 Then
gtotal = total2 - gtotal
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
Else
gtotal = gtotal - total2
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
End If

ElseIf testlast = "/" Then
If total1 = 0 And total2 = 0 And gtotal = 0 Then
outPut.Caption = "Divide Zero"
testlast = test
Else
If total1 = 0 And gtotal = 0 Then
total1 = 1
gtotal = 1
gtotal = (total2 / total1) / gtotal 'special because first instance
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 And total1 = 0 Then
total1 = 1
total2 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = "1"
Else
If total1 = 0 Then
total1 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 Then
total2 = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If gtotal = 0 Then
gtotal = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
End If
End If
End If
End If
End If
End If
End If
End If

End Sub

Private Sub math_LostFocus(Index As Integer)
If testlast = "=" Then
outPut.Caption = ""
total1 = 0
gtotal = 0
testlast = "1"
End If

End Sub

0

Commented:
sorry for double post
0

Author Commented:
I'm dead tired in trying to figure it out!
0

Commented:
is ur math button is for signs
then
as far as i understood ur code ,i think ur code can be like this
If testlast = "1" Or testlast = "=" Then

testlast = test

If testlast = "/" Then

if(total1=0)then
label1.caption = "Divide error or whatever"
end if
if this won't worked
plz post ur full code with comments as what button ur clicking for what action
0

Commented:
Hi GlobalFax it would be alot easier if I could run the code from vb without having to recreate everything. If you open the source file (.frm) with notepad and copy the code it would help
0

Author Commented:
math = divide "+, -, /, *, ="
calcBut = "1,2,3,4,5,6,7,8,9,0,."
Command1 = "CE"
Command2 = "+/-"
Clear = "C"

0

Commented:
and also you get the error cause you might be executing ur if loop before taking second value
so total1 is still 0
u should execute ur if loop only after clicking of "=" button
0

Author Commented:
=============================

VERSION 5.00
Begin VB.Form neg
Caption         =   "Unit 5GP - Calculator"
ClientHeight    =   3720
ClientLeft      =   60
ClientTop       =   345
ClientWidth     =   4995
ScaleHeight     =   3720
ScaleWidth      =   4995
StartUpPosition =   3  'Windows Default
Begin VB.CommandButton cmdExit
Caption         =   "E&xit"
Height          =   495
Left            =   3600
TabIndex        =   20
Top             =   3120
Width           =   1215
End
Begin VB.CommandButton math
Caption         =   "="
BeginProperty Font
Name            =   "MS Sans Serif"
Size            =   12
Charset         =   0
Weight          =   400
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   500
Index           =   4
Left            =   1680
TabIndex        =   19
Top             =   2640
Width           =   600
End
Begin VB.CommandButton Command2
Caption         =   "- / +"
BeginProperty Font
Name            =   "Arial Narrow"
Size            =   12
Charset         =   0
Weight          =   700
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   500
Left            =   3120
TabIndex        =   18
Top             =   2040
Width           =   600
End
Begin VB.CommandButton calcBut
Caption         =   "."
BeginProperty Font
Name            =   "Arial Narrow"
Size            =   15.75
Charset         =   0
Weight          =   700
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   500
Index           =   10
Left            =   240
TabIndex        =   17
Top             =   2640
Width           =   600
End
Begin VB.CommandButton Command1
Caption         =   "CE"
BeginProperty Font
Name            =   "Arial Narrow"
Size            =   12
Charset         =   0
Weight          =   700
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   500
Left            =   3120
TabIndex        =   16
Top             =   1440
Width           =   600
End
Begin VB.CommandButton math
Caption         =   "-"
BeginProperty Font
Name            =   "MS Sans Serif"
Size            =   12
Charset         =   0
Weight          =   400
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   500
Index           =   3
Left            =   2400
TabIndex        =   15
Top             =   2640
Width           =   600
End
Begin VB.CommandButton math
Caption         =   "*"
BeginProperty Font
Name            =   "MS Sans Serif"
Size            =   12
Charset         =   0
Weight          =   400
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   500
Index           =   2
Left            =   2400
TabIndex        =   14
Top             =   1440
Width           =   600
End
Begin VB.CommandButton math
Caption         =   "+"
BeginProperty Font
Name            =   "MS Sans Serif"
Size            =   12
Charset         =   0
Weight          =   400
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   500
Index           =   1
Left            =   2400
TabIndex        =   13
Top             =   2040
Width           =   600
End
Begin VB.CommandButton math
Caption         =   "/"
BeginProperty Font
Name            =   "MS Sans Serif"
Size            =   12
Charset         =   0
Weight          =   400
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   500
Index           =   0
Left            =   2400
TabIndex        =   12
Top             =   840
Width           =   600
End
Begin VB.CommandButton clear
Caption         =   "C"
BeginProperty Font
Name            =   "Arial Narrow"
Size            =   12
Charset         =   0
Weight          =   700
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   500
Left            =   3120
TabIndex        =   11
Top             =   840
Width           =   600
End
Begin VB.CommandButton calcBut
Caption         =   "9"
BeginProperty Font
Name            =   "Arial Narrow"
Size            =   15.75
Charset         =   0
Weight          =   700
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   500
Index           =   9
Left            =   1680
TabIndex        =   9
Top             =   840
Width           =   600
End
Begin VB.CommandButton calcBut
Caption         =   "8"
BeginProperty Font
Name            =   "Arial Narrow"
Size            =   15.75
Charset         =   0
Weight          =   700
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   500
Index           =   8
Left            =   960
TabIndex        =   8
Top             =   840
Width           =   600
End
Begin VB.CommandButton calcBut
Caption         =   "7"
BeginProperty Font
Name            =   "Arial Narrow"
Size            =   15.75
Charset         =   0
Weight          =   700
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   500
Index           =   7
Left            =   240
TabIndex        =   7
Top             =   840
Width           =   600
End
Begin VB.CommandButton calcBut
Caption         =   "6"
BeginProperty Font
Name            =   "Arial Narrow"
Size            =   15.75
Charset         =   0
Weight          =   700
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   500
Index           =   6
Left            =   1680
TabIndex        =   6
Top             =   1440
Width           =   600
End
Begin VB.CommandButton calcBut
Caption         =   "5"
BeginProperty Font
Name            =   "Arial Narrow"
Size            =   15.75
Charset         =   0
Weight          =   700
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   500
Index           =   5
Left            =   960
TabIndex        =   5
Top             =   1440
Width           =   600
End
Begin VB.CommandButton calcBut
Caption         =   "4"
BeginProperty Font
Name            =   "Arial Narrow"
Size            =   15.75
Charset         =   0
Weight          =   700
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   500
Index           =   4
Left            =   240
TabIndex        =   4
Top             =   1440
Width           =   600
End
Begin VB.CommandButton calcBut
Caption         =   "3"
BeginProperty Font
Name            =   "Arial Narrow"
Size            =   15.75
Charset         =   0
Weight          =   700
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   500
Index           =   3
Left            =   1680
TabIndex        =   3
Top             =   2040
Width           =   600
End
Begin VB.CommandButton calcBut
Caption         =   "2"
BeginProperty Font
Name            =   "Arial Narrow"
Size            =   15.75
Charset         =   0
Weight          =   700
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   500
Index           =   2
Left            =   960
TabIndex        =   2
Top             =   2040
Width           =   600
End
Begin VB.CommandButton calcBut
Caption         =   "1"
BeginProperty Font
Name            =   "Arial Narrow"
Size            =   15.75
Charset         =   0
Weight          =   700
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   500
Index           =   1
Left            =   240
TabIndex        =   1
Top             =   2040
Width           =   600
End
Begin VB.CommandButton calcBut
Caption         =   "0"
BeginProperty Font
Name            =   "Arial Narrow"
Size            =   15.75
Charset         =   0
Weight          =   700
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   500
Index           =   0
Left            =   960
TabIndex        =   0
Top             =   2640
Width           =   600
End
Begin VB.Label outPut
Alignment       =   1  'Right Justify
BackColor       =   &H80000009&
BorderStyle     =   1  'Fixed Single
BeginProperty Font
Name            =   "MS Sans Serif"
Size            =   13.5
Charset         =   0
Weight          =   700
Underline       =   0   'False
Italic          =   0   'False
Strikethrough   =   0   'False
EndProperty
Height          =   495
Left            =   240
TabIndex        =   10
Top             =   120
Width           =   4335
End
End
Attribute VB_Name = "neg"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim flag As Integer
Dim total1 As Double
Dim total1a As Double
Dim total2 As Double
Dim total2a As Double
Dim gtotal As Double
Dim gtotala As Double
Dim test As String
Dim testlast As String

Private Sub calcBut_Click(Index As Integer)
If total2 = 0 And total1 = 0 And gtotal = 0 Then
outPut.Caption = outPut.Caption & calcBut(Index).Caption
total1 = outPut.Caption
Else
If total1 = 0 Then
outPut.Caption = ""
outPut.Caption = outPut.Caption & calcBut(Index).Caption
total1 = outPut.Caption
Else
outPut.Caption = outPut.Caption & calcBut(Index).Caption
total1 = outPut.Caption
End If
End If
End Sub

Private Sub clear_Click()
outPut.Caption = ""
total1 = 0
gtotal = 0
testlast = "1"
End Sub

Private Sub cmdExit_Click()
End Sub

Private Sub Command1_Click()
outPut.Caption = gtotal
total1 = 0
total2 = 0
End Sub

Private Sub Command2_Click()
If outPut.Caption = "" Then
outPut.Caption = ""
Else
outPut.Caption = -(outPut.Caption)
gtotal = gtotal * -1

End If

End Sub

Top = (Screen.Height - neg.Height) / 2
Left = (Screen.Width - neg.Width) / 2
gtotal = 0
testlast = "1"
End Sub

Private Sub math_Click(Index As Integer)
total2 = total1
total1 = 0
test = math(Index).Caption

If testlast = "1" Or testlast = "=" Then

testlast = test

If test = "/" Then
If total1 = 0 And total2 = 0 And gtotal = 0 Then
outPut.Caption = "Divide Zero"
testlast = test
Else
If total1 = 0 And gtotal = 0 Then
total1 = 1
gtotal = 1
gtotal = (total2 / total1) / gtotal 'special because first instance
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 And total1 = 0 Then
total1 = 1
total2 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total1 = 0 Then
total1 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 Then
total2 = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If gtotal = 0 Then
gtotal = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
End If
End If
End If
End If
End If
End If

ElseIf test = "*" Then
If total1 = 0 And total2 = 0 Then
total2 = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
test = testlast
Else
If gtotal = 0 Then
gtotal = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
test = testlast
Else
If total2 = 0 Then
total2 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total1 = 0 Then
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
End If
End If
End If
End If

ElseIf test = "+" Then
gtotal = gtotal + total1 + total2
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test

ElseIf test = "-" Then
If gtotal = 0 Then
gtotal = total2 - gtotal
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
Else
gtotal = gtotal - total2
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
End If
End If
Else

If testlast = "*" Then
If total1 = 0 And total2 = 0 Then
total2 = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If gtotal = 0 Then
gtotal = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 Then
total2 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total1 = 0 Then
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
End If
End If
End If
End If

ElseIf testlast = "+" Then
gtotal = gtotal + total1 + total2
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test

ElseIf testlast = "-" Then
If gtotal = 0 Then
gtotal = total2 - gtotal
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
Else
gtotal = gtotal - total2
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
End If

ElseIf testlast = "/" Then
If total1 = 0 And total2 = 0 And gtotal = 0 Then
outPut.Caption = "Divide Zero"
testlast = test
Else
If total1 = 0 And gtotal = 0 Then
total1 = 1
gtotal = 1
gtotal = (total2 / total1) / gtotal 'special because first instance
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 And total1 = 0 Then
total1 = 1
total2 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = "1"
Else
If total1 = 0 Then
total1 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 Then
total2 = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If gtotal = 0 Then
gtotal = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
End If
End If
End If
End If
End If
End If
End If
End If

End Sub

Private Sub math_LostFocus(Index As Integer)
If testlast = "=" Then
outPut.Caption = ""
total1 = 0
gtotal = 0
testlast = "1"
End If

End Sub

0

Commented:
is this just code for a calculator?  What are the specifics of the calculator you are trying to make?
0

Author Commented:
Standard calculator
0

Commented:
replace that line with this:

If test = "/" And testlast <> "/" Then
0

Commented:
more specific:

Private Sub math_Click(Index As Integer)

total1 = 0
test = math(Index).Caption

If testlast = "1" Or testlast = "=" Then

testlast = test

If test = "/" And testlast <> "/" Then
0

Commented:
sorry that doesnt work
0

Author Commented:
ph66:

I did replace the line, but now if I divide any unmber by any number other than "0", I get no answer, just displays last number entered.
0

Commented:
X = total1
total2 = total1
total1 = 0
test = math(Index).Caption

If testlast = "1" Or testlast = "=" Then

testlast = test

If test = "/" Then
If total1 = 0 And total2 = 0 And gtotal = 0 Then
If math(Index).Caption <> "/" Then outPut.Caption = "Divide Zero"
testlast = test
Else
If total1 = 0 And gtotal = 0 Then
total1 = 1
gtotal = 1
gtotal = (total2 / total1) / gtotal 'special because first instance
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 And total1 = 0 Then
total1 = 1
total2 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total1 = 0 Then
total1 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 Then
total2 = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If gtotal = 0 Then
gtotal = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
End If
End If
End If
End If
End If
End If

ElseIf test = "*" Then
If total1 = 0 And total2 = 0 Then
total2 = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
test = testlast
Else
If gtotal = 0 Then
gtotal = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
test = testlast
Else
If total2 = 0 Then
total2 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total1 = 0 Then
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
End If
End If
End If
End If

ElseIf test = "+" Then
gtotal = gtotal + total1 + total2
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test

ElseIf test = "-" Then
If gtotal = 0 Then
gtotal = total2 - gtotal
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
Else
gtotal = gtotal - total2
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
End If
End If
Else

If testlast = "*" Then
If total1 = 0 And total2 = 0 Then
total2 = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If gtotal = 0 Then
gtotal = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 Then
total2 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total1 = 0 Then
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
End If
End If
End If
End If

ElseIf testlast = "+" Then
gtotal = gtotal + total1 + total2
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test

ElseIf testlast = "-" Then
If gtotal = 0 Then
gtotal = total2 - gtotal
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
Else
gtotal = gtotal - total2
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
End If

ElseIf testlast = "/" Then
If total1 = 0 And gtotal = 0 Then
outPut.Caption = "Divide Zero"
testlast = test
Else
If total1 = 0 And gtotal = 0 Then
total1 = 1
gtotal = 1
gtotal = (total2 / total1) / gtotal 'special because first instance
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 And total1 = 0 Then
total1 = 1
total2 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = "1"
Else
If total1 = 0 Then
total1 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 Then
total2 = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If gtotal = 0 Then
gtotal = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
End If
End If
End If
End If
End If
End If
End If
End If
0

Commented:
I believe the above works.

just change this:

If test = "/" Then
If total1 = 0 And total2 = 0 And gtotal = 0 Then
If math(Index).Caption <> "/" Then outPut.Caption = "Divide Zero"
testlast = test

and this:

ElseIf testlast = "/" Then
If total1 = 0 And gtotal = 0 Then
outPut.Caption = "Divide Zero"
testlast = test
Else
0

Author Commented:
ph66, excellent....now just need to get it to display "divide zero", when any number is divided by "0"
0

Author Commented:
btw:  in order to get the above working, I omitted the "x=total1"
0

Commented:
I also updated you calcbut file so you can enter "." then a number and then "+" and "." and another number without geting an error. (0.5+0.5=1)

Private Sub calcBut_Click(Index As Integer)
If total2 = 0 And total1 = 0 And gtotal = 0 Then
outPut.Caption = outPut.Caption & calcBut(Index).Caption

If outPut.Caption <> "." Then total1 = outPut.Caption

Else
If total1 = 0 Then
If outPut.Caption <> "." Then outPut.Caption = ""
outPut.Caption = outPut.Caption & calcBut(Index).Caption
If outPut.Caption <> "." Then total1 = outPut.Caption
Else
outPut.Caption = outPut.Caption & calcBut(Index).Caption
total1 = outPut.Caption
End If
End If
End Sub
0

Author Commented:
Also, I just noticed that when I divide "0" by any number, i.e. "0" divided by "3", on the display it shows "03"
0

Commented:
here try this:

the onlything I can find wrong with it is when you take a number like 9 / 0 it returns 9   'which Im sure can be fixed easy, let me know if you can not figure it out

Private Sub math_Click(Index As Integer)
total2 = total1
total1 = 0
test = math(Index).Caption

If testlast = "1" Or testlast = "=" Then

testlast = test

If test = "/" Then
If total1 = 0 And total2 = 0 And gtotal = 0 Then
If math(Index).Caption <> "/" Then outPut.Caption = "Divide Zero"
testlast = test
Else
If total1 = 0 And gtotal = 0 Then
total1 = 1
gtotal = 1
gtotal = (total2 / total1) / gtotal 'special because first instance
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 And total1 = 0 Then
total1 = 1
total2 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total1 = 0 Then
total1 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 Then
total2 = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If gtotal = 0 Then
gtotal = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
End If
End If
End If
End If
End If
End If

ElseIf test = "*" Then
If total1 = 0 And total2 = 0 Then
total2 = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
test = testlast
Else
If gtotal = 0 Then
gtotal = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
test = testlast
Else
If total2 = 0 Then
total2 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total1 = 0 Then
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
End If
End If
End If
End If

ElseIf test = "+" Then
gtotal = gtotal + total1 + total2
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test

ElseIf test = "-" Then
If gtotal = 0 Then
gtotal = total2 - gtotal
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
Else
gtotal = gtotal - total2
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
End If
End If
Else

If testlast = "*" Then
If total1 = 0 And total2 = 0 Then
total2 = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If gtotal = 0 Then
gtotal = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 Then
total2 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total1 = 0 Then
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
End If
End If
End If
End If

ElseIf testlast = "+" Then
gtotal = gtotal + total1 + total2
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test

ElseIf testlast = "-" Then
If gtotal = 0 Then
gtotal = total2 - gtotal
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
Else
gtotal = gtotal - total2
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
End If

ElseIf testlast = "/" Then
If total1 = 0 And total2 = 0 Then
outPut.Caption = "Divide Zero"
testlast = test
Else

If total1 = 0 And gtotal = 0 Then
total1 = 1
gtotal = 1
gtotal = (total2 / total1) / gtotal 'special because first instance
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 And total1 = 0 Then
total1 = 1
total2 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = "1"
Else
If total1 = 0 Then
total1 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 Then
total2 = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If gtotal = 0 Then
gtotal = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
End If
End If
End If
End If
End If
End If
End If
End If

End Sub
0

Commented:
Here try this: (if you run into trouble erase the stuff under calcbut() and replace it with the "'" stuff)

Option Explicit
Dim flag As Integer
Dim total1 As Double
Dim total1a As Double
Dim total2 As Double
Dim total2a As Double
Dim gtotal As Double
Dim gtotala As Double
Dim test As String
Dim testlast As String

Private Sub calcBut_Click(Index As Integer)
If total2 = 0 And total1 = 0 And gtotal = 0 Then
outPut.Caption = outPut.Caption & calcBut(Index).Caption

If outPut.Caption <> "." Then total1 = outPut.Caption

Else
If total1 = 0 Then
If outPut.Caption <> "." Then outPut.Caption = ""
outPut.Caption = outPut.Caption & calcBut(Index).Caption
If outPut.Caption <> "." Then total1 = outPut.Caption
Else
outPut.Caption = outPut.Caption & calcBut(Index).Caption
total1 = outPut.Caption
End If
End If

'If total2 = 0 And total1 = 0 And gtotal = 0 Then
'outPut.Caption = outPut.Caption & calcBut(Index).Caption
'total1 = outPut.Caption
'Else
'If total1 = 0 Then
'outPut.Caption = ""
'outPut.Caption = outPut.Caption & calcBut(Index).Caption
'total1 = outPut.Caption
'Else
'outPut.Caption = outPut.Caption & calcBut(Index).Caption
'total1 = outPut.Caption
'End If
'End If
End Sub

Private Sub clear_Click()
outPut.Caption = ""
total1 = 0
gtotal = 0
testlast = "1"
End Sub

Private Sub cmdExit_Click()
End Sub

Private Sub Command1_Click()
outPut.Caption = gtotal
total1 = 0
total2 = 0
End Sub

Private Sub Command2_Click()
If outPut.Caption = "" Then
outPut.Caption = ""
Else
outPut.Caption = -(outPut.Caption)
gtotal = gtotal * -1

End If

End Sub

Top = (Screen.Height - neg.Height) / 2
Left = (Screen.Width - neg.Width) / 2
gtotal = 0
testlast = "1"
End Sub

Private Sub math_Click(Index As Integer)
total2 = total1
total1 = 0
test = math(Index).Caption

If testlast = "1" Or testlast = "=" Then

testlast = test

If test = "/" Then
If total1 = 0 And total2 = 0 And gtotal = 0 Then
If math(Index).Caption <> "/" Then outPut.Caption = "Divide Zero"
testlast = test
Else
If total1 = 0 And gtotal = 0 Then
total1 = 1
gtotal = 1
gtotal = (total2 / total1) / gtotal 'special because first instance
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 And total1 = 0 Then
total1 = 1
total2 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total1 = 0 Then
total1 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 Then
total2 = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If gtotal = 0 Then
gtotal = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
End If
End If
End If
End If
End If
End If

ElseIf test = "*" Then
If total1 = 0 And total2 = 0 Then
total2 = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
test = testlast
Else
If gtotal = 0 Then
gtotal = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
test = testlast
Else
If total2 = 0 Then
total2 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total1 = 0 Then
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
End If
End If
End If
End If

ElseIf test = "+" Then
gtotal = gtotal + total1 + total2
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test

ElseIf test = "-" Then
If gtotal = 0 Then
gtotal = total2 - gtotal
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
Else
gtotal = gtotal - total2
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
End If
End If
Else

If testlast = "*" Then
If total1 = 0 And total2 = 0 Then
total2 = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If gtotal = 0 Then
gtotal = 1
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 Then
total2 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total1 = 0 Then
total1 = 1
gtotal = (total1 * total2) * gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
End If
End If
End If
End If

ElseIf testlast = "+" Then
gtotal = gtotal + total1 + total2
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test

ElseIf testlast = "-" Then
If gtotal = 0 Then
gtotal = total2 - gtotal
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
Else
gtotal = gtotal - total2
outPut.Caption = gtotal
total2 = 0
total1 = 0
testlast = test
End If

ElseIf testlast = "/" Then
If total1 = 0 And total2 = 0 Then
outPut.Caption = "Divide Zero"
testlast = test
Else

If total1 = 0 And gtotal = 0 Then
outPut.Caption = 0

Else
If total2 = 0 And total1 = 0 Then
total1 = 1
total2 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = "1"
Else
If total1 = 0 Then
total1 = 1
gtotal = gtotal / (total2 / total1)
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If total2 = 0 Then
total2 = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
Else
If gtotal = 0 Then
gtotal = 1
gtotal = (total2 / total1) / gtotal
outPut.Caption = gtotal
total1 = 0
total2 = 0
testlast = test
End If
End If
End If
End If
End If
End If
End If
End If

End Sub

Private Sub math_LostFocus(Index As Integer)
If testlast = "/" Then outPut.Caption = ""
If testlast = "=" Then
outPut.Caption = ""
total1 = 0
gtotal = 0
testlast = "1"
End If

End Sub
0

Commented:
if you want to display dive zero error when user clicks something / 0 then
modify ur code as follows

ElseIf testlast = "/" Then
If total1 = 0 Then 'And total2 = 0 And gtotal = 0 Then
outPut.Caption = "Divide Zero"
testlast = test
Else ......
0

Commented:
Avya2k -> that doesnt work because every time the answer is 0 you get a divide by zero (i think everytime)

9/9=divide by zero
5/5=divide by zero
0/ =divide by zero
0/0=error
.=error
8/1=0

the last code I submited works
0

Commented:
Hey GlobalFax did it work?
0

Author Commented:
ph66, my internet connection was out late last night and still this morning, I am at work and will try this as soon as I get home. thanks for your efforts as I'm sure your last suggestion will work.  will update you as soon as possible.
0

Author Commented:
ph66,

Awesome work to make my mess work as asked.....this last revision did the trick, thanks a million!

0

## Featured Post

• 14
• 12
• 4
• +1
Tackle projects and never again get stuck behind a technical roadblock.