Question Help for calculator

robbelineil

New member
Joined
Sep 2, 2013
Messages
3
Programming Experience
Beginner
hello guys, mind if you help me with my programming excercise?

my exercise is making a calculator, i managed to create my program with no errors and run it correctly but the problem is our professor asked us to add, subtract, multiply or divide more than TWO DIGITS. i only managed to make my program to compute for 2 digits only

mind if you help me guys?
 
Multiple operands

I think you mean two operands, not two digits.
You need to reuse one of the operands to continue computation, by processing the result with itself and the new value. For example, if doing addition:

Dim number, addend As Double
number = 5 ' 'first operand
addend = 3 ' 'second operand
number = number + addend ' 'result
addend = 2 ' 'add another number
number = number + addend ' 'next result
'etc.

For further assistance, you need to post your code.
 
Last edited:
I think you mean two operands, not two digits.
You need to reuse one of the operands to continue computation, by processing the result with itself and the new value. For example, if doing addition:

Dim number, addend As Double
number = 5 ' 'first operand
addend = 3 ' 'second operand
number = number + addend ' 'result
addend = 2 ' 'add another number
number = number + addend ' 'next result
'etc.

For further assistance, you need to post your code.


here is my code in my equals button and the operator buttons

Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click
val1 = txtDisp.Text
txtDisp.Clear()
choice = "plus"
End Sub


Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
If choice = "plus" Then
ans = val1 + Val(txtDisp.Text)
txtDisp.Text = ans
ElseIf choice = "subtract" Then
ans = val1 - Val(txtDisp.Text)
txtDisp.Text = ans
ElseIf choice = "multiply" Then
ans = val1 * Val(txtDisp.Text)
txtDisp.Text = ans
ElseIf choice = "divide" Then
ans = val1 / Val(txtDisp.Text)
txtDisp.Text = ans
End If


End Sub


Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
val1 = txtDisp.Text
txtDisp.Clear()
choice = "subtract"
End Sub


Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
val1 = txtDisp.Text
txtDisp.Clear()
choice = "multiply"
End Sub


Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
val1 = txtDisp.Text
txtDisp.Clear()
choice = "divide"
End Sub


Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
txtDisp.Clear()
End Sub
End Class
 
Hi,

Did you have a question there since this seems to be working OK?

The one bit of advice I can give you is to turn Option Strict On. This will help you to identify and correct all Type Conversion errors as you encounter them within your coding. To change this in your current project go to Project->Properties on the IDE's Menu and change the options on the Compile Tab and for all future projects you can do this by going to Tools->Options on the IDE's Menu and changing the VBDefaults.

Cheers,

Ian

BTW, when posting code, please do use the Advance button to add Code Tags around your code to help keep the formatting and to make things easier to read.
 
Hi,

Did you have a question there since this seems to be working OK?

The one bit of advice I can give you is to turn Option Strict On. This will help you to identify and correct all Type Conversion errors as you encounter them within your coding. To change this in your current project go to Project->Properties on the IDE's Menu and change the options on the Compile Tab and for all future projects you can do this by going to Tools->Options on the IDE's Menu and changing the VBDefaults.

Cheers,

Ian

BTW, when posting code, please do use the Advance button to add Code Tags around your code to help keep the formatting and to make things easier to read.

my only question sir is. how can i compute 3 or more operands. because in my current code i can only compute for two operands. for example. 1+1. but our professor requires us to make a calculator to compute for multiple operands. like 1+1+2+1-1 like that.

thanks in advance sir
 
Hi,

my only question sir is. how can i compute 3 or more operands. because in my current code i can only compute for two operands

The reason that I asked my question was because whether you realise it or not you can already compute for more than two operands so long as you declare your variables in the right place. I had a quick play this morning and I had to add three variable declarations at the Class level, being:-

VB.NET:
Private choice As String
Private val1 As Double
Private ans As Double

But once that was done all worked fine for multiple operands. Have you tried that yourself?

If you find that this now works, which it will, then turn Option Explicit On and the lesson learned for today will have been to always explicitly declare your variables in the correct place.

Hope that helps.

Cheers,

Ian
 
Back
Top