I have a problem with adding more than one number in calculator..the problem is while adding 1+2 answer is 3 and when i do 1+2+3 the answer is 5 and not 6 why so.. ihave tried different method of creating array for storing the value but nothing seems to work and i am getting a error while i type Value1 <> " THen. i get the error as conversion of string to type double is not valid.whereas i have already declared the Value1 As Double...please help

## Recommended Answers

Hi Arun welcome to DaniWeb.
Posting some relevant code would help us solve your problem.

For the error in conversion, that's means maybe the imputed data are taken as string or the problem is that you do not take care of the operations (signs {+,-,/,*}).

Coming to your actual problem of not getting the desired answer, this can be coursed by you not taking care …

## All 7 Replies

Hi Arun welcome to DaniWeb.
Posting some relevant code would help us solve your problem.

For the error in conversion, that's means maybe the imputed data are taken as string or the problem is that you do not take care of the operations (signs {+,-,/,*}).

Coming to your actual problem of not getting the desired answer, this can be coursed by you not taking care of the signs, if I were you I would declare a variable that will hold the operations so that they won't interfere with your calculations. If you had done that you would also declare variables to hold the values.

On mine I made mine to be able to hold 2 values and one operation, meaning the calculation is like this (1) (+) (1) right, on the left I have the first value and at the center I have the operation then on the right I have the value again so I would have 3 variables to hold value one, value 2, and then the operation and you would simply check which operation was used then process like this:

`````` If Operation = "+" Then
Answer = Value1 + Value2
Else
If Operation = "-" Then
Answer = Value1 + Value2
End If
End If
``````

Now coming to your problem you simple need to add another variables which will hold the third value and the second operation then proceed with your calculations.

@Mr M
In line5 you probably mean: Answer = Value1 - Value2 ?
@Arun
1) I would use the read out (textbox ?) also as a variable to keep on with the operations.
2) I would use select case stements for your operations code

Lacking code, I would hazard a guess that it is not doing a running calculation (1+2+3) but only displaying the result of the last operation (2+3).

Hi thanks for replying actually i tried all methods which u gave but it does works.i am posting some parts of my code and here it is
Private Sub ButAdd_Click(sender As Object, e As EventArgs) Handles ButAdd.Click

``````        If Value1 <> "" Then
Private Sub ButAdd_Click(sender As Object, e As EventArgs) Handles ButAdd.Click
If Value1 <> "" Then
Runningtotal = Runningtotal + Value1
End If
Add = True
Value1 = Val(TextBox1.Text)
End Sub

Private Sub ButEqu_Click(sender As Object, e As EventArgs) Handles ButEqu.Click
Value2 = Val(TextBox1.Text)
If Add = True Then
If Runningtotal = "" Then
Answer = Value1 + Value2
Else
Answer = Runningtotal + Value2
TextBox1.Text = Answer
End If
End If
End Sub

Runningtotal = Runningtotal + Value1
End If
Add = True
Value1 = Val(TextBox1.Text)
End Sub

Private Sub ButEqu_Click(sender As Object, e As EventArgs) Handles ButEqu.Click
Value2 = Val(TextBox1.Text)
If Add = True Then
If Runningtotal = "" Then
Answer = Value1 + Value2
Else
Answer = Runningtotal + Value2
TextBox1.Text = Answer
End If
End If
End Sub
here running Runningtotal is another variable which i have declared as a variable for storing the answer of prev calculation
``````

@Minimalist, yes thanks didn't notice that error while typing.

@Arun_14, I believe you can minimize the number of buttons by either placing your similar or relative codes on one button and separate by "IF" statement or even place them on a function.

Also instead of:

`````` If Add = True Then
``````

You need to do it like this:

`````` If Operation = ("your operation here") Then
' That was your first operation so do your first calculations here
RunningAnswer = Value1 (your operation here) Value2
If RunningAnswer IsNot Nothing And SecOperation = ("your second operation here") Then
Answer = RunningAnswer (your second operation here) SecValue2
' Then you can go on adding the else and repeat the above if statement
' To ensure you recognize all the operations.
End If
End If
``````

This is because the use may not always want to use both same operations, this will help if there are also a mixture of operation such as: 5*2-5 or 20/2+8, etc.

Here I just post here a simple example of calculation

``````Public Class Form1
Dim runningtotal As Double = 0.0#
Dim add As Boolean = False

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If add Then
runningtotal += Val(TextBox1.Text)

Else
If Not String.IsNullOrWhiteSpace(TextBox1.Text) Then
runningtotal = Val(TextBox1.Text)
End If

End If
add = True
Me.Text = runningtotal
TextBox1.Text = ""
TextBox1.Focus()

End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
runningtotal += Val(TextBox1.Text)
add = False
Me.Text = runningtotal
TextBox1.Text = ""
TextBox1.Focus()
End Sub

End Class
``````

Hope it can give you a way to solve your problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.