Ask Experts Questions for FREE Help !
Ask
    neo_lover2000's Avatar
    neo_lover2000 Posts: 12, Reputation: 1
    New Member
     
    #1

    Jun 1, 2009, 03:10 AM
    basic calculator
    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's Avatar
    Perito Posts: 3,139, Reputation: 150
    Ultra Member
     
    #2

    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's Avatar
    Perito Posts: 3,139, Reputation: 150
    Ultra Member
     
    #3

    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.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Visual basic code for calculator [ 8 Answers ]

I am a student at college in somaliland. One of my instructors told me to write a small programme for a calculator in visual basic 6.0. so, I'm stuck with this. Help me with the codes.

About basic calculator [ 2 Answers ]

I want to know what's the code for basic calculator, but what's I want to know more is about the operator "*+-/", what I want to do is what if I click in the first number then add it to the second number then add it again with the next number, the catch is when I click the + sign the second time it...

How to make a calculator in visual basic 6 [ 1 Answers ]

How to make a calculator in visual basic 6, what are the codes for it?

TI-84 Plus Calculator [ 1 Answers ]

This is sort of a math question and it isn't. I just bought this calculator yesterday, and the book that came with it does not tell me how to program the Quadratic formula as well as other equations into this calculator. And someone I know in my class has the same one and told me that this can be...

How to do e-0.05 on a calculator [ 5 Answers ]

Hi there, What are the exact steps I press on a calculator to do the following: - 40e-0.05 I keep getting the wrong answer? Any help is appreciated


View more questions Search