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

    Jun 7, 2009, 07:18 PM
    visual basic
    Dim first As Long
    Dim second As Long
    Dim sign As String
    Dim FirstValueEntered As Boolean


    Private Sub Command1_Click(Index As Integer)
    If FirstValueEntered = True Then
    TextDisplay.Text = TextDisplay.Text & Command1(Index).Caption
    second = Val(TextDisplay.Text)
    Else
    TextDisplay.Text = TextDisplay.Text & Command1(Index).Caption
    first = Val(TextDisplay.Text)
    End If
    End Sub

    Private Sub cmdPlus_Click()
    If (FirstValueEntered) Then
    first = first + second
    TextDisplay.Text = first
    second = 0
    ElseIf TextDisplay.Text <> "" Then
    FirstValueEntered = True
    sign = cmdPlus.Caption
    TextDisplay.Text = ""
    End If
    End Sub

    Private Sub cmdMinus_Click()
    If (FirstValueEntered) Then
    first = first - second
    TextDisplay.Text = first
    second = 0
    ElseIf TextDisplay.Text <> "" Then
    FirstValueEntered = True
    sign = cmdMinus.Caption
    End If
    End Sub

    Private Sub cmdC_Click()
    TextDisplay.Text = ""
    FirstValueEntered = False
    End Sub

    Private Sub cmdEq_Click()
    If TextDisplay.Text <> "" Then
    second = Val(TextDisplay.Text)
    Else
    second = 0
    End If
    If sign = "+" Then
    TextDisplay = first + second
    ElseIf sign = "-" Then
    TextDisplay = first - second
    Else
    TextDisplay = "?"
    End If
    FirstValueEntered = False
    End Sub


    I would like you to check this program.
    1. if I press a number ex 1 twice it display 11
    2. if I press + once it gives command for the operation it will perform
    3. if I press a number ex 2 twice it display 22
    4. if I press + it will display the sum of the first two number,then it gives again command for the operation it will perform,
    5. if I press a number ex 1,it is appended on the number displayed, ex 33 the sum of the first two number what happened is 331.
    how can I fix it.
    thanks.
    Perito's Avatar
    Perito Posts: 3,139, Reputation: 150
    Ultra Member
     
    #2

    Jun 8, 2009, 11:22 AM

    The fundamental problem is that you have too many program states and no way to distinguish them.

    1. Nothing has been entered
    2. One number has been entered and a second is being entered.
    3. A second number has been entered and a third is being entered.
    4. = has been pressed and there is a value on the display and the user will enter a new number.

    In #2, the TextDisplay box is empty when this state starts.
    In #3, the TextDisplay box is not empty when this state starts.
    You need to recognize the program states and differentiate the program's operation. One idea would be to have a variable that would be set depending on the program's states (an integer). Here's another way to do it, just by adding another Boolean (it's a bit more kludgy than I would like).

    Option Explicit

    Dim first As Long
    Dim second As Long
    Dim sign As String
    Dim FirstValueEntered As Boolean
    Dim SecondValueEntered As Boolean

    Private Sub Command1_Click(Index As Integer)
    If FirstValueEntered = True Then
    If SecondValueEntered Then
    textDisplay.Text = Command1(Index).Caption
    SecondValueEntered = False
    Else
    textDisplay.Text = textDisplay.Text & Command1(Index).Caption
    End If
    second = Val(textDisplay.Text)
    Else
    If SecondValueEntered Then
    textDisplay.Text = Command1(Index).Caption
    SecondValueEntered = False
    Else
    textDisplay.Text = textDisplay.Text & Command1(Index).Caption
    End If
    first = Val(textDisplay.Text)
    End If
    End Sub

    Private Sub cmdPlus_Click()
    If (FirstValueEntered) Then
    first = first + second
    textDisplay.Text = first
    second = 0
    SecondValueEntered = True
    ElseIf textDisplay.Text <> "" Then
    FirstValueEntered = True
    sign = cmdPlus.Caption
    textDisplay.Text = ""
    End If
    End Sub

    Private Sub cmdMinus_Click()
    If (FirstValueEntered) Then
    first = first - second
    textDisplay.Text = first
    second = 0
    ElseIf textDisplay.Text <> "" Then
    FirstValueEntered = True
    sign = cmdMinus.Caption
    End If
    End Sub

    Private Sub cmdC_Click()
    textDisplay.Text = ""
    FirstValueEntered = False
    End Sub

    Private Sub cmdEq_Click()
    If textDisplay.Text <> "" Then
    second = Val(textDisplay.Text)
    Else
    second = 0
    End If
    If sign = "+" Then
    textDisplay = first + second
    ElseIf sign = "-" Then
    textDisplay = first - second
    Else
    textDisplay = "?"
    End If
    FirstValueEntered = False
    SecondValueEntered = True
    End Sub

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 [ 1 Answers ]

Actually I'm a new user(student) of Visual Basic.So I want to know how I am make a simple calculator in Visual Basic 6?

Visual basic dot net [ 1 Answers ]

:rolleyes: What is the dot net technology? What is the use of this technology with Visual basic? What is the concept of DOT net PASSPORT?

Visual Basic 6.0 [ 1 Answers ]

What is the cost of Visual Studio 6.0 Enterprise Edition?

Visual Basic [ 1 Answers ]

I want to add icon in the button which I have used in my visual basic program. How can I do that. Please help.


View more questions Search