Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculator Help
#1
Hello, BP community.

Well, I made a calculator in VB10, but there is a problem. The "." button shows the wrong results. For example, 12.5 + 12.5 = 24,5.

The code snippet I use for the dot button is this one:
Code:
x = x + 1
If x = 1 Then
   txtMain.AppendText(".")
Else
   'Do Nothing
End If

And the code snippet I use for calculating is this:
Code:
Try
  If Operation = "+" Then
      Answer = Number + Val(txtMain.Text)
      History.Items.Add(Number + " + " + txtMain.Text + " = " + Answer)

      txtMain.Clear()
      txtMain.Text = Answer
      x = 0
   ElseIf Operation = "-" Then
      Answer = Number - Val(txtMain.Text)
      History.Items.Add(Number + " - " + txtMain.Text + " = " + Answer)

      txtMain.Clear()
      txtMain.Text = Answer
      x = 0
  ElseIf Operation = "*" Then
      Answer = Number * Val(txtMain.Text)
      History.Items.Add(Number + " * " + txtMain.Text + " = " + Answer)

      txtMain.Clear()
      txtMain.Text = Answer
      x = 0
  ElseIf Operation = "/" Then
      Answer = Number / Val(txtMain.Text)
      History.Items.Add(Number + " / " + txtMain.Text + " = " + Answer)

      txtMain.Clear()
      txtMain.Text = Answer
      x = 0
  End If
Catch ex As Exception
  MsgBox(ex.Message)
End Try

I gave you those two codes because you maybe need the calculating code, as well, to edit it for me. Maybe you need an explanation with that "x" variable. It actually prevents "." to be in one text box twice.

I have no idea if you understood the x variable, but it is not actually so important (for you). I hope I get response soon. Thanks.

Regards,
Vinwarez.
Also known as Rocketalypse.
System.out.println("I prefer Java.");
#2
Well, it looks like what your program is doing is turning one of the 12.5's into an integer, which cannot contain a decimal. So it may be turning
12.5 + 12.5 = 24.5
Into
12.5 + 12 = 24.5.

So, You are going to need to check the following line of code:
Code:
Answer = Number + Val(txtMain.Text)

To see if either Answer, Number, Or Val return integer variables, and if they do, they need to be changed to double variables.
My Blog | My Setup | My Videos | Have a wonderful day.
#3
Oh! Whenever I declare a variable, for some reason, if it isn't a string, I declare it as integer. Now I know that with calculator I shouldn't do that. Thank you!

I've tried to make Answer and Number variable as double, but it still isn't working.

Here is how I declared it:
Code:
Public Memory, Operation As String, Number, Answer As Double, x As Integer

Val is not a variable that I declared, it already exists in VB. I have no idea what I did wrong. I have no idea what to do. When I google it, it just shows the results which contain tutorials with all buttons except the one I need. Eh.

Regards,
Vinwarez.
Also known as Rocketalypse.
System.out.println("I prefer Java.");
#4
Hm, alright. Well since I don't know exactly what's wrong.. let's rule out the option of Val returning a variable of type integer by replacing all instances of
Code:
Val(txtMain.Text)

With..

Code:
CDbl(Val(txtMain.Text))
My Blog | My Setup | My Videos | Have a wonderful day.
#5
Nope. Not working. I think it is hard to help me with this if you actually do not look at the whole code. I will send you the project file over PM. Thanks!

Regards,
Vinwarez.
Also known as Rocketalypse.
System.out.println("I prefer Java.");
#6
Alright, so since the variable Number seems to be a String... I changed the following line:
Code:
Answer = Number + CDbl(Val(txtMain.Text))

To

Code:
Answer = CDbl(Val(Number)) + CDbl(Val(txtMain.Text))

And that seems to fix the problem. Let me know!
My Blog | My Setup | My Videos | Have a wonderful day.
#7
Hey, Brandonio! Thank you so much!
Also known as Rocketalypse.
System.out.println("I prefer Java.");
#8
Glad that I could help!
My Blog | My Setup | My Videos | Have a wonderful day.


Possibly Related Threads…
Thread Author Replies Views Last Post
  I need help making a calculator like a windows calculator Xandereliteinsight 4 17,921 07-24-2012, 07:53 AM
Last Post: Moldraxian

Forum Jump:


Users browsing this thread: 1 Guest(s)