?
Solved

Help!   Simple Calculator project bug when dividing by zero

Posted on 2003-03-19
33
Medium Priority
?
168 Views
Last Modified: 2010-05-01
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
Comment
Question by:GlobalFax
[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
  • 14
  • 12
  • 4
  • +1
33 Comments
 
LVL 2

Expert Comment

by:Jacamar
ID: 8171633
lets see a bit more code.....i'm sure it's a simple fix
0
 
LVL 2

Expert Comment

by:Jacamar
ID: 8171637
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
 
LVL 1

Author Comment

by:GlobalFax
ID: 8171650
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()
Unload neg
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



Private Sub Form_Load()
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
Independent Software Vendors: 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!

 
LVL 2

Expert Comment

by:Jacamar
ID: 8171672
what are total1, 2 and 3 for?
0
 
LVL 1

Expert Comment

by:ph66
ID: 8171681
please post the source from the .frm file (include the souce that creates the buttons, labels etc...)
0
 
LVL 1

Expert Comment

by:ph66
ID: 8171693
please post the source from the .frm file (include the souce that creates the buttons, labels etc...)
0
 
LVL 1

Author Comment

by:GlobalFax
ID: 8171696
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()
Unload neg
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



Private Sub Form_Load()
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
 
LVL 1

Expert Comment

by:ph66
ID: 8171697
sorry for double post
0
 
LVL 1

Author Comment

by:GlobalFax
ID: 8171700
I'm dead tired in trying to figure it out!
0
 
LVL 5

Expert Comment

by:avya2k
ID: 8171718
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
 
LVL 1

Expert Comment

by:ph66
ID: 8171723
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
 
LVL 1

Author Comment

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

sorry about lack of comments!
0
 
LVL 5

Expert Comment

by:avya2k
ID: 8171730
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
 
LVL 1

Author Comment

by:GlobalFax
ID: 8171737
Here it is from notepad:
=============================


VERSION 5.00
Begin VB.Form neg
   Caption         =   "Unit 5GP - Calculator"
   ClientHeight    =   3720
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   4995
   LinkTopic       =   "Form1"
   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()
Unload neg
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



Private Sub Form_Load()
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
 
LVL 2

Expert Comment

by:Jacamar
ID: 8171740
is this just code for a calculator?  What are the specifics of the calculator you are trying to make?
0
 
LVL 1

Author Comment

by:GlobalFax
ID: 8171754
Standard calculator
0
 
LVL 1

Expert Comment

by:ph66
ID: 8171775
replace that line with this:

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

Expert Comment

by:ph66
ID: 8171777
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
 
LVL 1

Expert Comment

by:ph66
ID: 8171784
sorry that doesnt work
0
 
LVL 1

Author Comment

by:GlobalFax
ID: 8171822
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
 
LVL 1

Expert Comment

by:ph66
ID: 8171835
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
 
LVL 1

Expert Comment

by:ph66
ID: 8171847
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
 
LVL 1

Author Comment

by:GlobalFax
ID: 8171882
ph66, excellent....now just need to get it to display "divide zero", when any number is divided by "0"
0
 
LVL 1

Author Comment

by:GlobalFax
ID: 8171891
btw:  in order to get the above working, I omitted the "x=total1"
0
 
LVL 1

Expert Comment

by:ph66
ID: 8171900
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
 
LVL 1

Author Comment

by:GlobalFax
ID: 8171906
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
 
LVL 1

Expert Comment

by:ph66
ID: 8171951
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
 
LVL 1

Accepted Solution

by:
ph66 earned 2000 total points
ID: 8171978
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()
Unload neg
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



Private Sub Form_Load()
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
 
LVL 5

Expert Comment

by:avya2k
ID: 8171980
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
 
LVL 1

Expert Comment

by:ph66
ID: 8172103
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
 
LVL 1

Expert Comment

by:ph66
ID: 8172146
Hey GlobalFax did it work?
0
 
LVL 1

Author Comment

by:GlobalFax
ID: 8173707
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
 
LVL 1

Author Comment

by:GlobalFax
ID: 8177679
ph66,

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

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

770 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