CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: can't calculate

  1. #1
    Join Date
    Sep 1999
    Location
    Baytown, TX, United States
    Posts
    23

    can't calculate

    I have the following code:
    [vbcode]Private Sub cmdCalc_Click()
    Const strMsg As String = "The number of pennies was "
    Dim sngDollars As Single, sngQuarters As Integer, sngDimes As Integer, sngNickels As Integer, sngNPennies As Integer
    Dim intPennies As Integer
    'assign values to variables
    intPennies = Val(txtPennies.Text)
    sngDollars = Val(lblNdollars.Caption)
    sngQuarters = Val(lblNquarters.Caption)
    sngDimes = Val(lblNdimes.Caption)
    sngNickels = Val(lblNnickels.Caption)
    sngNPennies = Val(lblNpennies.Caption)
    'perform calculations
    sngDollars = Val(intPennies) / 100
    sngQuarters = Val(lblNdollars.Caption) - intPennies / 25
    sngDimes = Val(lblNquarters.Caption) - intPennies / 10
    sngNickels = Val(lblNdimes.Caption intPennies / 5
    sngNPennies = Val(intPennies)
    lblMsg.Caption = strMsg & Format(intPennies, "Number of Pennies")
    End Sub

    Private Sub Form_Load()
    Const conPrompt As String = "Enter the number of pennies"
    Const conTitle As String = "Number of Pennies"
    Dim intPennies As Integer
    intPennies = Val(InputBox("Enter the number of pennies", "Number of Pennies"))
    'center the form
    frmJpennies.Top = (Screen.Height - frmJpennies.Height) / 2
    frmJpennies.Left = (Screen.Width - frmJpennies.Width) / 2
    End Sub

    I unable to calculate the number of pennies. AM I doing something wrong? I KNOW this is the right formula. Furthermore, when I enter the number of pennies into the input box, it doesn't show up on the form.

    kazooie21

  2. #2
    Join Date
    Apr 1999
    Location
    Brooklyn, NY USA
    Posts
    171

    Re: can't calculate

    Use CInt, CSng and so on instead of Val, like this:
    intPennies = CInt(txtPennies.Text)
    sngDollars = CSng(lblNdollars.Caption)
    Vlad



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured