By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,946 Members | 1,714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,946 IT Pros & Developers. It's quick & easy.

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

P: 2
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
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.     Private first As Double
  4.     Private second As Double
  5.     Private oper As String
  6.     Private negpos As Boolean
  7.  
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  2.         first = txtoutput.Text
  3.         txtoutput.Clear()
  4.         oper = "+"
  5.  
  6.     End Sub
  7.  
  8.     Private Sub btnEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEqual.Click
  9.         second = txtoutput.Text
  10.         If (oper = "+") Then
  11.             txtoutput.Text = first + second
  12.         Else
  13.             If (oper = "-") Then
  14.                 txtoutput.Text = first - second
  15.             Else
  16.                 If (oper = "*") Then
  17.                     txtoutput.Text = first * second
  18.                 Else
  19.                     If (oper = "/") Then
  20.                         txtoutput.Text = first / second
  21.                     Else
  22.                         If (oper = "s") Then
  23.                             txtoutput.Text = System.Math.Sqrt(first)
  24.                         Else
  25.                             If (oper = "%") Then
  26.                                 txtoutput.Text = (first * second) / 100
  27.                             End If
  28.                         End If
  29.                     End If
  30.                 End If
  31.             End If
  32.         End If
  33.     End Sub
  34.  
  35.     Private Sub btnSubstract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubstract.Click
  36.         first = txtoutput.Text
  37.         txtoutput.Clear()
  38.         oper = "-"
  39.     End Sub
  40.  
  41.     Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
  42.         first = txtoutput.Text
  43.         txtoutput.Clear()
  44.         oper = "*"
  45.     End Sub
  46.  
  47.     Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
  48.         first = txtoutput.Text
  49.         txtoutput.Clear()
  50.         oper = "/"
  51.     End Sub
  52.  
  53.     Private Sub btnSqrt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSqrt.Click
  54.         first = txtoutput.Text
  55.         txtoutput.Clear()
  56.         oper = "s"
  57.     End Sub
  58.  
  59.     Private Sub btnPercent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPercent.Click
  60.         first = txtoutput.Text
  61.         oper = "%"
  62.     End Sub
  63. End Class
  64.  
Sep 23 '10 #1
Share this Question
Share on Google+
2 Replies


Joseph Martell
Expert 100+
P: 196
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.
Sep 29 '10 #2

!NoItAll
100+
P: 296
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...
Expand|Select|Wrap|Line Numbers
  1. txtoutput.Text = (first - second).ToString
  2. txtoutput.Text = System.Math.Sqrt(first).ToString
  3.  
VB will do the conversion for you - but it's bad form.
Oct 2 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.