Click here to Skip to main content
15,886,813 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got every other operation button to work besides sqrt, and percentage. I've done the same thing for addition, subtraction, etc. and it doesn't seem to work.

here's my globals
VB
Public Class Form1
    Private first As Double
    Private second As Double
    Private oper As String
    Private negpos As Boolean


VB
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        first = txtoutput.Text
        txtoutput.Clear()
        oper = "+"
    End Sub
    Private Sub btnEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEqual.Click
        second = txtoutput.Text
        If (oper = "+") Then
            txtoutput.Text = first + second
        Else
            If (oper = "-") Then
                txtoutput.Text = first - second
            Else
                If (oper = "*") Then
                    txtoutput.Text = first * second
                Else
                    If (oper = "/") Then
                        txtoutput.Text = first / second
                    Else
                        If (oper = "s") Then
                            txtoutput.Text = System.Math.Sqrt(first)
                        Else
                            If (oper = "%") Then
                                txtoutput.Text = (first * second) / 100
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End Sub
    Private Sub btnSubstract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubstract.Click
        first = txtoutput.Text
        txtoutput.Clear()
        oper = "-"
    End Sub
    Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
        first = txtoutput.Text
        txtoutput.Clear()
        oper = "*"
    End Sub
    Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
        first = txtoutput.Text
        txtoutput.Clear()
        oper = "/"
    End Sub
    Private Sub btnSqrt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSqrt.Click
        first = txtoutput.Text
        txtoutput.Clear()
        oper = "s"
    End Sub
    Private Sub btnPercent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPercent.Click
        first = txtoutput.Text
        oper = "%"
    End Sub
End Class
Posted

First of all I would like to recommend using select case

select case oper
  case '+'
    txtoutput.Text = first + second
  case '-'
    txtoutput.Text = first - second
 ...
end select


this makes the code shorter and also better readable. Also you might consider to lose the magic pushbutton. It would be more easy to test if you would simply create a function like:

public function Calculate(byval first as string, byval second as string, byval oper as string) as string


You could easily test this function, even without a user interface just simply in the direct window.

The actions of the buttons also do look a like and you could simply create a more generic implementation but this is up to you.

The reason why it isn't working is because your code is not completely properly written. You get a string value from the textbox and use it as number in various calculations. Basic has always been a very forgiving language and still looks the other way in most cases. You could try to wrap the calculation something like Convert.ToString(System.Math.Sqrt(first)). Also remember that to execute the square root you must enter the number, click s and then click equal to work.

Hopefully this will get you on your way. If you encounter any more problems, just let me know.

Good luck!
 
Share this answer
 
First of all....you might want to look into using a case statement. It can turn all of your crazy nested if statements into this much easier to read statement:
VB
Select Case oper
            Case "+"
                txtoutput.Text = first + second
            Case "-"
                 txtoutput.Text = first - second
            Case "*"
                txtoutput.Text = first * second
            Case "/"
                txtoutput.Text = first / second
            Case "s"
                txtoutput.Text = System.Math.Sqrt(first)
            Case "%"
                txtoutput.Text = (first * Second()) / 100
End Select


Second, the only problem I can see is that if you are trying to do the square root you don't want to fill the second variable when the equal button is pressed because it will probably be empty.

If you need more help, please provide more information about how it isn't working, what error messages you get and/or what scenario you are trying that is giving an incorrect result.
 
Share this answer
 
v2
Wrong Implementation of Logic

For Sqrt & %

for Ex: 1000-10%=900

it shows =1 as answer Please Help ASAP
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900