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
+ 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 Public Class Form1       Private first As Double     Private second As Double     Private oper As String     Private negpos As Boolean   Expand|Select|Wrap|Line Numbers 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   Sep 23 '10 #1
Share this Question
2 Replies

 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

 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 txtoutput.Text = (first - second).ToString txtoutput.Text = System.Math.Sqrt(first).ToString   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.