How do i get my sqrt button and percentage button to work on my calculator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RoxyBeginner
    New Member
    • Jul 2010
    • 2

    How do i get my sqrt button and percentage button to work on my calculator

    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
    Code:
    Public Class Form1
    
        Private first As Double
        Private second As Double
        Private oper As String
        Private negpos As Boolean
    Code:
    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
  • Joseph Martell
    Recognized Expert New Member
    • Jan 2010
    • 198

    #2
    First, what exactly is going wrong? How are your square root and percent functions not working correctly? Are exceptions being thrown or are the results just off?

    Second, and this is mostly a question of personal preference and not functionality, you could use a select case statement instead of all of those nested if's. It would be functionally the same as your current code, but it might be easier to read.

    Comment

    • !NoItAll
      Contributor
      • May 2006
      • 297

      #3
      Yeah - I'm going with Joseph. A Select Case section would be a much better way to handle this.
      As far as assigning the output of your calculations to the text control you should also make sure you are assigning a string.
      Some quick examples of how to do this...
      Code:
      txtoutput.Text = (first - second).ToString
      txtoutput.Text = System.Math.Sqrt(first).ToString
      VB will do the conversion for you - but it's bad form.

      Comment

      Working...