PDA

View Full Version : Basic calculator


neo_lover2000
Jun 1, 2009, 03:10 AM
I already finish a very simple calculator,actually it has only four buttons.
at first its working fine but when add another feature its not working anymore.

I will describe first the appearance of my calculator, its has a button for number 1, plus button, equals button, clear button, it has four text box, the textDisplay for the result. Num1.text to display first number, num2.text to display second number and flag.text to display temporary value(sum of first and second number).

what I want to do now is to display the sum of first and second number on the textDisplay when I press + button then getting ready to enter new number and so on.
for example: 1+1+1+1...

to all master there I am getting confuse on setting a flag can you please explain how to set and how it works?

I attaché my program.
if the process is 1+1 = result, I make it work but when I added new process I'm lost.

thanks

Dim first As Long
Dim second As Long
Dim sign As String
Dim Calculated As Boolean

Private Sub Command1_Click()

TextDisplay.Text = TextDisplay.Text + Command1.Caption

End Sub

Private Sub cmdPlus_Click()
Calculated = True

If Calculated = True Then
first = Val(TextDisplay.Text)
TextDisplay.Text = ""
second = Val(TextDisplay.Text)
first = first + second
TextDisplay.Text = first
second = 0

Else
first = Val(TextDisplay.Text)
TextNum1.Text = first
TextDisplay.Text = ""
sign = cmdPlus.Caption

End If
End Sub

Private Sub cmdC_Click()
TextDisplay.Text = ""
TextNum1.Text = ""
TextNum2.Text = ""
TextFlag.Text = ""
End Sub

Private Sub cmdEq_Click()
second = Val(TextDisplay.Text)
TextNum2.Text = second
If sign = "+" Then
TextDisplay = first + second
End If
End Sub

Perito
Jun 1, 2009, 07:11 AM
I am getting confused on setting a flag. Can you please explain how to set and how it works?


I don't understand your question.

"Setting a flag" usually means setting a Boolean variable to True or False. I think your "Calculated" variable must be the flag you're referring to. As it's written now, this subroutine:

Private Sub cmdPlus_Click()
Calculated = True

If Calculated = True Then
first = Val(TextDisplay.Text)
TextDisplay.Text = ""
second = Val(TextDisplay.Text)
first = first + second
TextDisplay.Text = first
second = 0

Else
first = Val(TextDisplay.Text)
TextNum1.Text = first
TextDisplay.Text = ""
sign = cmdPlus.Caption

End If
End Sub

The "else" clause will never be executed. Can you explain a little better what the program flow in this subroutine is supposed to do?

Perito
Jun 1, 2009, 07:12 AM
I think this is an idea of what you want to do with the "Calculated" flag:

Private Sub cmdPlus_Click()
' Don't set 'Calculated = True' here

If Calculated = True Then
first = Val(textDisplay.Text)
textDisplay.Text = ""
second = Val(textDisplay.Text)
first = first + second
textDisplay.Text = first
second = 0
Calculated = False ' You need to set it False somewhere. I'm thinking this is where you want to do it.
Else
first = Val(textDisplay.Text)
Textnum1.Text = first
textDisplay.Text = ""
sign = cmdPlus.Caption
End If
End Sub

Private Sub cmdC_Click()
textDisplay.Text = ""
Textnum1.Text = ""
Textnum2.Text = ""
Textflag.Text = ""
Calculated = True ' Note: Set the "Calculated" flag here.
End Sub

Private Sub cmdEq_Click()
second = Val(textDisplay.Text)
Textnum2.Text = second
If sign = "+" Then
textDisplay = first + second
End If
Calculated = True ' Note: Also set the "Calculated" flag here.
End Sub



I'm sure I can help you figure out how to do this, but I'm still not sure what you're trying to do. Give me a specific example. For example:

1. Click the "1" button twice. "11" will appear in the TextDisplay box.
2. Click the "+" button once. (state what you want to go where).
3. Click the "1" button again. (state what you want to go where).
4. Click the "=" button (state what you want to go where).
5. Click the "C" button. All boxes are emptied.