-
Oct 11th, 2015, 08:54 AM
#1
Thread Starter
Junior Member
My code for a calculator dosent work please help! thanks
Hello, I was supposed to make a calculator for my assignment but when I wrote the code it didn't seem to work and the = button didn't work properly. it would be greatly appreciated if you could help...
Here is the code:
Public Class Form1
Dim FirstNumber As Single
Dim SecondNumber As Single
Dim Answer As Single
Dim Operation As String
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
TextBox1.AppendText(4)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.AppendText(1)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.AppendText(2)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
TextBox1.AppendText(3)
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
TextBox1.AppendText(5)
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
TextBox1.AppendText(6)
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
TextBox1.AppendText(7)
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
TextBox1.AppendText(8)
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
TextBox1.AppendText(9)
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
TextBox1.AppendText(0)
End Sub
Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
FirstNumber = Val(TextBox1.Text)
TextBox1.Text = "+"
Operation = "+"
End Sub
Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
FirstNumber = Val(TextBox1.Text)
TextBox1.Text = "0"
Operation = "-"
End Sub
Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
FirstNumber = Val(TextBox1.Text)
TextBox1.Text = "0"
Operation = "*"
End Sub
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
FirstNumber = Val(TextBox1.Text)
TextBox1.Text = "0"
Operation = "/"
End Sub
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
If Operation = "+" Then
Answer = FirstNumber + SecondNumber
End If
If Operation = "-" Then
Answer = FirstNumber - SecondNumber
End If
If Operation = "*" Then
Answer = FirstNumber * SecondNumber
End If
If Operation = "/" Then
Answer = FirstNumber / SecondNumber
End If
TextBox1.Text = Answer
End Sub
Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
TextBox1.Clear()
End Sub
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
TextBox1.AppendText(".")
End Sub
End Class
-
Oct 11th, 2015, 10:09 AM
#2
Re: My code for a calculator dosent work please help! thanks
Have you debugged your code? That should be your first course of action. Don't just read your code; watch it in action. Place a breakpoint at the top of the section you want to debug (F9) and then step through the code line by line (F10). At each step, use the tools available to determine whether the application state is what you expect. As soon as it isn't, you have found an issue and you can investigate that specifically.
-
Oct 11th, 2015, 10:24 AM
#3
Thread Starter
Junior Member
Re: My code for a calculator dosent work please help! thanks
Hello, Thanks for replying. I just tried what you said and I debugged it and did what you said but what happens is that when you press the "=" Button it bring the first value that you entered. I don't know why it does this but it would be great if I could get some help.
basically if you pressed 45 + 5 when you press the = button it will write 45 in the textbox
-
Oct 11th, 2015, 11:24 AM
#4
Re: My code for a calculator dosent work please help! thanks
So, the thing to do is to put a breakpoint somewhere. My first thought would be to go right to the source of the problem, but it would actually be a bit better to start earlier. Therefore, I'd put the breakpoint on this line:
If Operation = "+" Then
At that point, all the variables will be filled. You can hover the mouse over both FirstNumber then SecondNumber and see what is in each one. You can then step through the addition to see that it is actually happening, except that you won't get that far, because as soon as you hover the mouse over SecondNumber you'll see the problem: It's zero, so the addition of the FirstNumber plus the SecondNumber will equal the FirstNumber.
So, why is that? Probably because you never set SecondNumber to anything anywhere in the code, as far as I can see.
My usual boring signature: Nothing
-
Oct 11th, 2015, 12:42 PM
#5
Thread Starter
Junior Member
Re: My code for a calculator dosent work please help! thanks
shaggy hiker, thanks for replying! I will look into it now and let you know how it goes!
-
Oct 11th, 2015, 12:48 PM
#6
Thread Starter
Junior Member
Re: My code for a calculator dosent work please help! thanks
Shaggy you are 100% correct the value of the first number was the number I entered, but the value of the second number was "0.0"! But Why??????? can you help me assign a value to it t, I am only a beginner....your help would be much appreciated!...............I have assigned a value but I must press the "=" button twice for it to add up and if you keep pressing it then it will just continue!
If Operation = "+" Then
Answer = FirstNumber + SecondNumber
SecondNumber = TextBox1.Text
End If
many thanks
Last edited by The flyingDutchmen; Oct 11th, 2015 at 12:54 PM.
-
Oct 11th, 2015, 01:07 PM
#7
Re: My code for a calculator dosent work please help! thanks
Actually, the second number is certainly more difficult. After all, the way you have the code working, when you press an operator button, the first number is removed from the textbox and the operator is shown. So....how do you even enter a second number? You can't just type more numbers, because the operator is already in there. I guess that could work, it would just look kind of ugly. You might type in 123 +, at which point the textbox should be showing "+". If you then typed a 4, the textbox would then show "+4", and so forth. That would be ok, it just wouldn't look great.
If you look at the way the calculator in Windows works, you see a more complex solution, which would require a label and a textbox on a panel, probably, and you may not want to go that far. So, what DO you want it to do? If you just go ahead and concatenate numbers onto the operator and only deal with it when you press =, that would be easier, though a bit goofy looking. However, in that case you would have to strip that first character off the textbox using .SubString to get the number (which would be all the characters after the operator), then put that into SecondNumber prior to doing the calculation (in what you just showed, you tried to fill SecondNumber immediately after you had done the calculation, which isn't going to work because you need the number in there before the calculation...and you can't just convert the text to a number as it stands because the operator will be in there).
So, the real key is figuring out what you want it to do. There are LOADS of examples of creating calculators on the forum, because this seems to be a regular assignment in programming classes, so you might search on "Calculator" and see what you find.
My usual boring signature: Nothing
-
Oct 11th, 2015, 01:22 PM
#8
Thread Starter
Junior Member
Re: My code for a calculator dosent work please help! thanks
I have cracked it!!!!!!!!!!!!!
If Operation = "+" Then
Answer = FirstNumber + SecondNumber
SecondNumber = TextBox1.Text
Answer = FirstNumber + TextBox1.Text
End If
however the equal button is continues if the user keeps clicking it, can you help with that. By the way thanks for the help really appreciate it
-
Oct 12th, 2015, 10:49 AM
#9
Re: My code for a calculator dosent work please help! thanks
I don't know why you didn't just set the second number before the operations.
Code:
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
SecondNumber = TextBox1.Text
If Operation = "+" Then
Answer = FirstNumber + SecondNumber
End If
If Operation = "-" Then
Answer = FirstNumber - SecondNumber
End If
If Operation = "*" Then
Answer = FirstNumber * SecondNumber
End If
If Operation = "/" Then
Answer = FirstNumber / SecondNumber
End If
TextBox1.Text = Answer
End Sub
If you want to handle the "=" correctly, then you probably want to set a flag when you hit equal the first time, and not set secondNumber (you want to continue to reuse secondNumber with each = pressed). Instead you would want to set FirstNumber to TextBox1.Text, so your operation acts on your total.
Code:
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
If EqualActive Then
FirstNumber = TextBox1.Text
Else
SecondNumber = TextBox1.Text
End If
EqualActive = True 'Set so next time if equal pressed twice or more in a row, we do the same operation on the total
'...
The issue with the above, is that you need to reset EqualActive back to False in every other button click (which would be a good reason to use an array of controls so you could have a common routine to handle all buttons, or call a common function from each of your button subs to append the text or set the operation and clear the flag.
-
Oct 12th, 2015, 11:11 AM
#10
Thread Starter
Junior Member
Re: My code for a calculator dosent work please help! thanks
Hello passel, sorry I don't seem to be following what you say I am just a beginner so if you could explain it I a way that I could understand that would be appreciated. however I do see the concept that you are trying to tell me, but just don't understand it
many thanks mo
-
Oct 12th, 2015, 03:52 PM
#11
Re: My code for a calculator dosent work please help! thanks
Well, an example of calling a common function.
Each button that would append text to the textbox now calls a sub called append, passing the string to be appended.
That way the check for the EqualActive flag being cleared only has to be done in one place, rather than 11 places.
Note that I had to turn Option Strict Off (I have it on by default) because technically your coding is not the way it should be.
But since you're just beginning, I don't feel like getting into the multiple things that should be done differently. You might want to turn Option String On (at the top of your source), or set it in your IDE settings, so it is always On.
You also might want to use Doubles rather than Singles, since Singles will tap out around 7 digits of significance (has to switch to Scientific aka exponential output to show large numbers, and is truncating values). Doubles will get you out to around 13 significant digits before truncating.
vb.net Code:
Option Strict Off Public Class Form1 Dim FirstNumber As Single Dim SecondNumber As Single Dim Answer As Single Dim Operation As String Dim EqualActive As Boolean Private Sub Append(c As String) If EqualActive Then '= was active, starting a new number so clear textbox TextBox1.Clear() EqualActive = False 'We hit something other than "=", so clear flag End If If TextBox1.Text = "0" Then 'If we have a 0 in the textbox, overwrite it If c <> "." Then TextBox1.Text = c Else TextBox1.AppendText(c) 'Else append End If Else TextBox1.AppendText(c) 'Else append End If End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Append(4) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Append(1) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Append(2) End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Append(3) End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click Append(5) End Sub Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click Append(6) End Sub Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click Append(7) End Sub Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click Append(8) End Sub Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click Append(9) End Sub Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click Append(0) End Sub Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click FirstNumber = Val(TextBox1.Text) TextBox1.Text = "0" Operation = "+" End Sub Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click FirstNumber = Val(TextBox1.Text) TextBox1.Text = "0" Operation = "-" End Sub Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click FirstNumber = Val(TextBox1.Text) TextBox1.Text = "0" Operation = "*" End Sub Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click FirstNumber = Val(TextBox1.Text) TextBox1.Text = "0" Operation = "/" End Sub Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click If EqualActive Then FirstNumber = TextBox1.Text 'we will do the operation on total and SecondNumber Else SecondNumber = TextBox1.Text 'we will do the operation on firstNumber and SecondNumber End If EqualActive = True 'In case we hit = again, repeat last function on total If Operation = "+" Then Answer = FirstNumber + SecondNumber End If If Operation = "-" Then Answer = FirstNumber - SecondNumber End If If Operation = "*" Then Answer = FirstNumber * SecondNumber End If If Operation = "/" Then Answer = FirstNumber / SecondNumber End If TextBox1.Text = Answer End Sub Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click TextBox1.Text = 0 FirstNumber = 0 SecondNumber = 0 End Sub Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click Append(".") End Sub End Class
To shorten the code, you could also handle some repetitive code by having a common Event handler for multiple buttons, and converting the Sender Object to a button so you can look at its properties.
Also, if I have a number mutually exclusive If statements (like your operation process), I would probably put them in a Select Case Block.
Code:
Select Case Operation
Case "+"
Answer = FirstNumber + SecondNumber
Case "-"
Answer = FirstNumber - SecondNumber
Case "*"
Answer = FirstNumber * SecondNumber
Case "/"
Answer = FirstNumber / SecondNumber
End Select
And, though some may disapprove, if the cases are all short one liners, I would use the statement separator ":", and put each case on a single line, as done in the code below.
vb.net Code:
Option Strict Off Public Class Form1 Dim FirstNumber As Double Dim SecondNumber As Double Dim Answer As Double Dim Operation As String Dim EqualActive As Boolean Private Sub Append(c As String) If EqualActive Then '= was active, starting a new number so clear textbox TextBox1.Clear() EqualActive = False 'We hit something other than "=", so clear flag End If If TextBox1.Text = "0" Then 'If we have a 0 in the textbox, overwrite it If c <> "," Then TextBox1.Text = c Else TextBox1.AppendText(c) 'Else append End If Else TextBox1.AppendText(c) 'Else append End If End Sub Private Sub Button_Append_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click, Button10.Click, Button11.Click 'Handle 0 to 9 and . Dim bt As Button = CType(sender, Button) 'Cast the sender object, to a button type Append(bt.Text) 'Append the text that is on the button to the textbox End Sub Private Sub Button_Operation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click, Button14.Click, Button15.Click, Button16.Click 'handles +,-,*,/ Dim bt As Button = CType(sender, Button) FirstNumber = Val(TextBox1.Text) TextBox1.Text = "0" Operation = bt.Text End Sub Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click If EqualActive Then FirstNumber = TextBox1.Text 'we will do the operation on total and SecondNumber Else SecondNumber = TextBox1.Text 'we will do the operation on firstNumber and SecondNumber End If EqualActive = True 'In case we hit = again, repeat last function on total Select Case Operation Case "+" : Answer = FirstNumber + SecondNumber Case "-" : Answer = FirstNumber - SecondNumber Case "*" : Answer = FirstNumber * SecondNumber Case "/" : Answer = FirstNumber / SecondNumber End Select TextBox1.Text = Answer End Sub Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click TextBox1.Text = 0 FirstNumber = 0 SecondNumber = 0 End Sub End Class
Control arrays are more useful for updating a lot of controls with common code, or loops, but since you don't have that situation here, I won't go into that.
Last edited by passel; Oct 12th, 2015 at 04:09 PM.
-
Oct 12th, 2015, 04:00 PM
#12
Thread Starter
Junior Member
Re: My code for a calculator dosent work please help! thanks
OMG passel! I do not know what to say!!! you are amazing man thanks for the help! day in day out I have been posting but no replies but you? you are Great!!! Thanks soooo much! will let you know how I got on with my assignment. Hoping for an A but with this I think I will go way above that
-
Oct 12th, 2015, 05:04 PM
#13
Re: My code for a calculator dosent work please help! thanks
What do you mean by "day in and day out I have been posting"? You only have 10 posts and 7 of them are in this thread, which was only started yesterday? Do you have a bunch of missing posts? The first few do have a delay to them because they are reviewed to see whether or not you are a human or a spam-bot, so they can be delayed in showing up.
My usual boring signature: Nothing
-
Oct 12th, 2015, 05:17 PM
#14
Thread Starter
Junior Member
Re: My code for a calculator dosent work please help! thanks
Shaggy hiker! yeah I have had a lot more post but just don't know why they are not coming up here! I have done more than 20 posts for sure but they keep disappearing somehow I don't know how??? but anyway it means a lot to find the solution to my problem!! once again many thanks to all!!!
-
Oct 12th, 2015, 06:39 PM
#15
Re: My code for a calculator dosent work please help! thanks
If the posts that vanished were all early posts, then it could just be that 'heavier' moderation that happens up until something like post 3 or 5, or something like that. It can baffle people early on, but ultimately it's a good thing, since there are plenty of spam bots on patrol. PM one of the mods if you are curious about those 'missing' posts.
My usual boring signature: Nothing
-
Oct 13th, 2015, 05:01 AM
#16
Thread Starter
Junior Member
Re: My code for a calculator dosent work please help! thanks
Thanks for the reply I now know where they all disappeared!!!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|