Results 1 to 16 of 16

Thread: My code for a calculator dosent work please help! thanks

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2015
    Posts
    20

    Question 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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    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.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2015
    Posts
    20

    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

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2015
    Posts
    20

    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!

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2015
    Posts
    20

    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.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2015
    Posts
    20

    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

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    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.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Oct 2015
    Posts
    20

    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

  11. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    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:
    1. Option Strict Off
    2.  
    3. Public Class Form1
    4.  
    5.   Dim FirstNumber As Single
    6.   Dim SecondNumber As Single
    7.   Dim Answer As Single
    8.   Dim Operation As String
    9.   Dim EqualActive As Boolean
    10.  
    11.   Private Sub Append(c As String)
    12.     If EqualActive Then                  '= was active, starting a new number so clear textbox
    13.       TextBox1.Clear()
    14.       EqualActive = False                  'We hit something other than "=", so clear flag
    15.     End If
    16.  
    17.     If TextBox1.Text = "0" Then          'If we have a 0 in the textbox, overwrite it
    18.       If c <> "." Then
    19.         TextBox1.Text = c
    20.       Else
    21.         TextBox1.AppendText(c)             'Else append
    22.       End If
    23.     Else
    24.       TextBox1.AppendText(c)             'Else append
    25.     End If
    26.   End Sub
    27.  
    28.   Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    29.     Append(4)
    30.   End Sub
    31.  
    32.   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    33.     Append(1)
    34.   End Sub
    35.  
    36.   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    37.     Append(2)
    38.   End Sub
    39.  
    40.   Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    41.     Append(3)
    42.   End Sub
    43.  
    44.   Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    45.     Append(5)
    46.   End Sub
    47.  
    48.   Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    49.     Append(6)
    50.   End Sub
    51.  
    52.   Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    53.     Append(7)
    54.   End Sub
    55.  
    56.   Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
    57.     Append(8)
    58.   End Sub
    59.  
    60.   Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
    61.     Append(9)
    62.   End Sub
    63.  
    64.   Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
    65.     Append(0)
    66.   End Sub
    67.  
    68.   Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
    69.     FirstNumber = Val(TextBox1.Text)
    70.     TextBox1.Text = "0"
    71.     Operation = "+"
    72.   End Sub
    73.  
    74.   Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
    75.     FirstNumber = Val(TextBox1.Text)
    76.     TextBox1.Text = "0"
    77.     Operation = "-"
    78.   End Sub
    79.  
    80.   Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
    81.     FirstNumber = Val(TextBox1.Text)
    82.     TextBox1.Text = "0"
    83.     Operation = "*"
    84.   End Sub
    85.  
    86.   Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
    87.     FirstNumber = Val(TextBox1.Text)
    88.     TextBox1.Text = "0"
    89.     Operation = "/"
    90.   End Sub
    91.  
    92.   Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
    93.     If EqualActive Then
    94.       FirstNumber = TextBox1.Text 'we will do the operation on total and SecondNumber
    95.     Else
    96.       SecondNumber = TextBox1.Text 'we will do the operation on firstNumber and SecondNumber
    97.     End If
    98.     EqualActive = True                  'In case we hit = again, repeat last function on total
    99.  
    100.     If Operation = "+" Then
    101.       Answer = FirstNumber + SecondNumber
    102.     End If
    103.     If Operation = "-" Then
    104.       Answer = FirstNumber - SecondNumber
    105.     End If
    106.     If Operation = "*" Then
    107.       Answer = FirstNumber * SecondNumber
    108.     End If
    109.     If Operation = "/" Then
    110.       Answer = FirstNumber / SecondNumber
    111.     End If
    112.     TextBox1.Text = Answer
    113.   End Sub
    114.  
    115.   Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
    116.     TextBox1.Text = 0
    117.     FirstNumber = 0
    118.     SecondNumber = 0
    119.   End Sub
    120.  
    121.   Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
    122.     Append(".")
    123.   End Sub
    124. 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:
    1. Option Strict Off
    2.  
    3. Public Class Form1
    4.  
    5.   Dim FirstNumber As Double
    6.   Dim SecondNumber As Double
    7.   Dim Answer As Double
    8.   Dim Operation As String
    9.   Dim EqualActive As Boolean
    10.  
    11.   Private Sub Append(c As String)
    12.     If EqualActive Then                  '= was active, starting a new number so clear textbox
    13.       TextBox1.Clear()
    14.       EqualActive = False                  'We hit something other than "=", so clear flag
    15.     End If
    16.  
    17.     If TextBox1.Text = "0" Then          'If we have a 0 in the textbox, overwrite it
    18.       If c <> "," Then
    19.         TextBox1.Text = c
    20.       Else
    21.         TextBox1.AppendText(c)             'Else append
    22.       End If
    23.     Else
    24.       TextBox1.AppendText(c)             'Else append
    25.     End If
    26.   End Sub
    27.  
    28.   Private Sub Button_Append_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click,
    29.               Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click,
    30.               Button9.Click, Button10.Click, Button11.Click
    31.     'Handle 0 to 9 and .
    32.     Dim bt As Button = CType(sender, Button)  'Cast the sender object, to a button type
    33.     Append(bt.Text)                           'Append the text that is on the button to the textbox
    34.   End Sub
    35.  
    36.   Private Sub Button_Operation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click,
    37.               Button14.Click, Button15.Click, Button16.Click
    38.     'handles +,-,*,/
    39.     Dim bt As Button = CType(sender, Button)
    40.     FirstNumber = Val(TextBox1.Text)
    41.     TextBox1.Text = "0"
    42.     Operation = bt.Text
    43.   End Sub
    44.  
    45.   Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
    46.     If EqualActive Then
    47.       FirstNumber = TextBox1.Text 'we will do the operation on total and SecondNumber
    48.     Else
    49.       SecondNumber = TextBox1.Text 'we will do the operation on firstNumber and SecondNumber
    50.     End If
    51.     EqualActive = True                  'In case we hit = again, repeat last function on total
    52.  
    53.     Select Case Operation
    54.       Case "+" : Answer = FirstNumber + SecondNumber
    55.       Case "-" : Answer = FirstNumber - SecondNumber
    56.       Case "*" : Answer = FirstNumber * SecondNumber
    57.       Case "/" : Answer = FirstNumber / SecondNumber
    58.     End Select
    59.  
    60.     TextBox1.Text = Answer
    61.   End Sub
    62.  
    63.   Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
    64.     TextBox1.Text = 0
    65.     FirstNumber = 0
    66.     SecondNumber = 0
    67.   End Sub
    68.  
    69. 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.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Oct 2015
    Posts
    20

    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

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Oct 2015
    Posts
    20

    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!!!

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Oct 2015
    Posts
    20

    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
  •  



Click Here to Expand Forum to Full Width