TriTechnotic TriTechnotic - 1 month ago 11
Vb.net Question

VB Calculator Displaying 0 at all times, and incorrect if/then/else results

I'm trying to make a full calculator using Visual Basic, and I've seem to run into the following error. When I hit any operation button I get the wrong result from the if/then/else statements. instead of assigning all of the booleans etc. it gives me the error message that was only supposed to appear with user error, ie hitting an operation twice etc. After commenting this portion out, I found another error, after hitting equals the answer was always zero, and I'm unsure of why. Thanks for the help.

Public Class frmCalc
Dim num1 As Decimal = 0
Dim num2 As Decimal = 0
Dim answer As Decimal = 0
Dim cnt As Integer = 10
Dim add As Boolean = False
Dim mult As Boolean = False
Dim div As Boolean = False
Dim subt As Boolean = False
Dim second As Boolean = False
Dim dec As Boolean = False
Dim crash As String = "Error!"
Dim uCrash As String = "User Error."
Dim first As Boolean = True
Public Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
If dec = False Then
If first = True Then
num1 = (num1 * 10) + 1
ElseIf second = True Then
num2 = (num2 * 10) + 1
Else
MessageBox.Show(crash)
Application.Exit()
End If
ElseIf dec = True Then
If first = True Then
num1 = num1 + (1 / cnt)
ElseIf second = True Then
num2 = num2 + (1 / cnt)
Else
MessageBox.Show(crash)
Application.Exit()
End If
cnt *= 10
Else
MessageBox.Show(crash)
Application.Exit()
End If
End Sub

Public Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
If dec = False Then
If first = True Then
num1 = (num1 * 10) + 2
ElseIf second = True Then
num2 = (num2 * 10) + 2
Else
MessageBox.Show(crash)
Application.Exit()
End If
ElseIf dec = True Then
If first = True Then
num1 = num1 + (2 / cnt)
ElseIf second = True Then
num2 = num2 + (2 / cnt)
Else
MessageBox.Show(crash)
Application.Exit()
End If
cnt *= 10
Else
MessageBox.Show(crash)
Application.Exit()
End If
End Sub

Public Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
If dec = False Then
If first = True Then
num1 = (num1 * 10) + 3
ElseIf second = True Then
num2 = (num2 * 10) + 3
Else
MessageBox.Show(crash)
Application.Exit()
End If
ElseIf dec = True Then
If first = True Then
num1 = num1 + (3 / cnt)
ElseIf second = True Then
num2 = num2 + (3 / cnt)
Else
MessageBox.Show(crash)
Application.Exit()
End If
cnt *= 10
Else
MessageBox.Show(crash)
Application.Exit()
End If
End Sub

Public Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
If (add = True) Or (subt = True) Or (mult = True) Or (div = True) Then
add = True
subt = False
mult = False
div = False
dec = False
first = False
second = True
cnt = 10
Else
MessageBox.Show(uCrash)
Application.Exit()
End If
End Sub

Public Sub btn4_Click(sender As Object, e As EventArgs) Handles btn4.Click
If dec = False Then
If first = True Then
num1 = (num1 * 10) + 4
ElseIf second = True Then
num2 = (num2 * 10) + 4
Else
MessageBox.Show(crash)
Application.Exit()
End If
ElseIf dec = True Then
If first = True Then
num1 = num1 + (4 / cnt)
ElseIf second = True Then
num2 = num2 + (4 / cnt)
Else
MessageBox.Show(crash)
Application.Exit()
End If
cnt *= 10
Else
MessageBox.Show(crash)
Application.Exit()
End If
End Sub

Public Sub btn5_Click(sender As Object, e As EventArgs) Handles btn5.Click
If dec = False Then
If first = True Then
num1 = (num1 * 10) + 5
ElseIf second = True Then
num2 = (num2 * 10) + 5
Else
MessageBox.Show(crash)
Application.Exit()
End If
ElseIf dec = True Then
If first = True Then
num1 = num1 + (5 / cnt)
ElseIf second = True Then
num2 = num2 + (5 / cnt)
Else
MessageBox.Show(crash)
Application.Exit()
End If
cnt *= 10
Else
MessageBox.Show(crash)
Application.Exit()
End If
End Sub

Public Sub btn6_Click(sender As Object, e As EventArgs) Handles btn6.Click
If dec = False Then
If first = True Then
num1 = (num1 * 10) + 6
ElseIf second = True Then
num2 = (num2 * 10) + 6
Else
MessageBox.Show(crash)
Application.Exit()
End If
ElseIf dec = True Then
If first = True Then
num1 = num1 + (6 / cnt)
ElseIf second = True Then
num2 = num2 + (6 / cnt)
Else
MessageBox.Show(crash)
Application.Exit()
End If
cnt *= 10
Else
MessageBox.Show(crash)
Application.Exit()
End If
End Sub

Public Sub btnMult_Click(sender As Object, e As EventArgs) Handles btnMult.Click
If (add = True) Or (subt = True) Or (mult = True) Or (div = True) Then
add = False
subt = False
mult = True
div = False
dec = False
second = True
cnt = 10
Else
MessageBox.Show(uCrash)
Application.Exit()
End If
End Sub

Public Sub btn7_Click(sender As Object, e As EventArgs) Handles btn7.Click
If dec = False Then
If first = True Then
num1 = (num1 * 10) + 7
ElseIf second = True Then
num2 = (num2 * 10) + 7
Else
MessageBox.Show(crash)
Application.Exit()
End If
ElseIf dec = True Then
If first = True Then
num1 = num1 + (7 / cnt)
ElseIf second = True Then
num2 = num2 + (7 / cnt)
Else
MessageBox.Show(crash)
Application.Exit()
End If
cnt *= 10
Else
MessageBox.Show(crash)
Application.Exit()
End If
End Sub

Public Sub btn8_Click(sender As Object, e As EventArgs) Handles btn8.Click
If dec = False Then
If first = True Then
num1 = (num1 * 10) + 8
ElseIf second = True Then
num2 = (num2 * 10) + 8
Else
MessageBox.Show(crash)
Application.Exit()
End If
ElseIf dec = True Then
If first = True Then
num1 = num1 + (8 / cnt)
ElseIf second = True Then
num2 = num2 + (8 / cnt)
Else
MessageBox.Show(crash)
Application.Exit()
End If
cnt *= 10
Else
MessageBox.Show(crash)
Application.Exit()
End If
End Sub

Public Sub btn9_Click(sender As Object, e As EventArgs) Handles btn9.Click
If dec = False Then
If first = True Then
num1 = (num1 * 10) + 9
ElseIf second = True Then
num2 = (num2 * 10) + 9
Else
MessageBox.Show(crash)
Application.Exit()
End If
ElseIf dec = True Then
If first = True Then
num1 = num1 + (9 / cnt)
ElseIf second = True Then
num2 = num2 + (9 / cnt)
Else
MessageBox.Show(crash)
Application.Exit()
End If
cnt *= 10
Else
MessageBox.Show(crash)
Application.Exit()
End If
End Sub

Public Sub btnDiv_Click(sender As Object, e As EventArgs) Handles btnDiv.Click
If (add = True) Or (subt = True) Or (mult = True) Or (div = True) Then
add = False
subt = False
mult = False
div = True
dec = False
second = True
cnt = 10
Else
MessageBox.Show(uCrash)
Application.Exit()
End If
End Sub

Public Sub btnDec_Click(sender As Object, e As EventArgs) Handles btnDec.Click
dec = True
End Sub

Public Sub btn0_Click(sender As Object, e As EventArgs) Handles btn0.Click
If dec = False Then
If first = True Then
num1 = (num1 * 10)
ElseIf second = True Then
num2 = (num2 * 10)
Else
MessageBox.Show(crash)
Application.Exit()
End If
ElseIf dec = True Then
cnt *= 10
Else
MessageBox.Show(crash)
Application.Exit()
End If
End Sub

Public Sub btnEquals_Click(sender As Object, e As EventArgs) Handles btnEquals.Click
If add = True Then
answer = num1 + num2
ElseIf subt = True Then
answer = num1 - num2
ElseIf div = True Then
answer = num1 / num2
ElseIf mult = True Then
answer = num1 * num2
Else
MessageBox.Show(uCrash)
Application.Exit()
End If
MessageBox.Show(answer)
num1 = answer
num2 = 0
first = False
second = False
dec = False
End Sub

Public Sub btnSub_Click(sender As Object, e As EventArgs) Handles btnSub.Click
If (add = True) Or (subt = True) Or (mult = True) Or (div = True) Then
add = False
subt = True
mult = False
div = False
dec = False
second = True
cnt = 10
Else
MessageBox.Show(uCrash)
Application.Exit()
End If
End Sub

Public Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
num1 = 0
num2 = 0
answer = 0
cnt = 10
first = True
second = False
mult = False
add = False
subt = False
div = False
dec = False
End Sub

Public Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
If first = True Then
num1 = num1 - (num1 Mod 10)
ElseIf second = True Then
num2 = num2 - (num2 Mod 10)
Else
MessageBox.Show(crash)
Application.Exit()
End If
End Sub

End Class

Answer

Your logic seems reversed. If add = True when an operator is pressed, it should give an error because it means that you already pressed add, not the other way around:

If (add = True) Or (subt = True) Or (mult = True) Or (div = True) Then
        MessageBox.Show(uCrash)
        Application.Exit()
    Else
        add = False
        subt = False
        mult = False
        div = True
        dec = False
        second = True
        cnt = 10
    End If

edit: I think that check could be replaced by (second=true), too.

Comments