im pretty new to vb6. i have two problems with my calculator. first, when i divide by zero
the calculator does not show the error message i have set it show, instead it shows 0.
my second problem is that after i click the equal sign, and click a number, the number is added on to the answer
instead of clearing the answer and putting in the number.
for example, if i do 9+9, 18 shows after i click the equal sign. if i click 1 instead of showing 1, it shows 181. help is much needed. here is my whole calcultor code.

Dim calcSaved As Double, calcResult As Double, calcOp As String

Private Sub cmdCalc_Click(Index As Integer)
    Dim calcBut As String, calcDisplay As String
    calcBut = cmdCalc(Index).Caption
    calcDisplay = lblCalc.Caption
  
    
    Select Case calcBut
        Case "0", "1", "2", "3", "4", _
              "5", "6", "7", "8", "9"
            calcDisplay = calcDisplay & calcBut
            lblCalc.Caption = calcDisplay
        Case "."
            If (InStr(calcDisplay, ".") > 0) Then
                calcDisplay = calcDisplay & calcBut
            Else
                calcDisplay = calcDisplay & "."
            End If
            lblCalc.Caption = calcDisplay
        Case "+/-"
            If (calcDisplay <> "") Then
                If Left(calcDisplay, 1) = "-" Then
                    calcDisplay = Replace(calcDisplay, "-", "")
                Else
                    calcDisplay = "-" & calcDisplay
                End If
            End If
            lblCalc.Caption = calcDisplay
        Case "+", "-", "x", "/"
            calcSaved = Val(calcDisplay)
            lblCalc.Caption = ""
            calcOp = calcBut
        Case "="
            calcResult = Val(calcDisplay)
            Select Case calcOp
                Case "+"
                    calcResult = calcResult + calcSaved
                Case "-"
                    calcResult = calcSaved - calcResult
                Case "x"
                    calcResult = calcSaved * calcResult
                Case "/"
                    If calcResult = 0 Then
                       
                       calcDisplay = "error"
                    Else
                    calcResult = calcSaved / calcResult
                 
                    End If
                Case Else
                    calcResult = calcDisplay
            End Select
            lblCalc.Caption = calcResult
        Case "BackSpace"
            If Val(calcDisplay) <> 0 Then
                calcDisplay = Left(calcDisplay, Len(calcDisplay) - 1)
                calcSaved = Val(calcDisplay)
                lblCalc.Caption = calcDisplay
            End If
        Case "Clear"
            calcDisplay = ""
            calcResult = 0
            calcSaved = 0
            lblCalc.Caption = calcDisplay
    End Select

    
    
        
End Sub

Recommended Answers

All 2 Replies

Hi,

change the code where u r displaying the result :
Check this :

if trim(calDisplay) ="error" then
   lblcalc.caption ="error"
else
    lblCalc.Caption = calcResult
end if

and after every "=" operation keep a Boolean Flag and If the Next button pressed is not "+-/*", then clear the contents

Regards
Veena

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.