CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 1999
    Location
    Baytown, TX, United States
    Posts
    23

    still can't calculate

    I have change my code, replacing all Vals with CInt and CSng, but it still won't calculate:

    Option Explicit


    Private Sub cmdCalc_Click()
    Dim sngDollars As Single, sngQuarters As Single, sngDimes As Single, sngNickels As Single, sngNPennies As Single
    Dim intPennies As Integer
    'assign values to variables
    intPennies = Val(InputBox("Enter the number of pennies", "Number of Pennies"))
    sngDollars = CSng(lblNdollars.Caption)
    sngQuarters = CSng(lblNquarters.Caption)
    sngDimes = CSng(lblNdimes.Caption)
    sngNickels = CSng(lblNnickels.Caption)
    sngNPennies = CSng(lblNpennies.Caption)
    'perform calculations
    sngDollars = CSng(intPennies) / 100
    sngQuarters = CSng(lblNdollars.Caption) - CSng(intPennies) / 25
    sngDimes = CSng(lblNquarters.Caption) - CSng(intPennies) / 10
    sngNickels = CSng(lblNdimes.Caption) - CSng(intPennies) / 5
    sngNPennies = CSng(intPennies) / 1
    End Sub



    Private Sub cmdClear_Click()
    'clear the screen for next amount
    lblNdollars.Caption = ""
    lblNquarters.Caption = ""
    lblNdimes.Caption = ""
    lblNnickels.Caption = ""
    lblNpennies.Caption = ""
    End Sub


    Private Sub cmdExit_Click()
    End
    End Sub

    Private Sub cmdPrint_Click()
    'hide command buttons before printing the form
    cmdCalc.Visible = False
    cmdPrint.Visible = False
    cmdClear.Visible = False
    cmdExit.Visible = False
    'display command buttons after the form is printed
    cmdCalc.Visible = True
    cmdPrint.Visible = True
    cmdClear.Visible = True
    cmdExit.Visible = True
    cmdClear.SetFocus
    End Sub
    End Sub

    Private Sub Form_Load()
    'center the form
    frmJpennies.Top = (Screen.Height - frmJpennies.Height) / 2
    frmJpennies.Left = (Screen.Width - frmJpennies.Width) / 2
    End Sub



    kazooie21

  2. #2
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: still can't calculate

    Try this:

    private Sub cmdCalc_Click()
    Dim iDollars as Long
    Dim iQuarters as Long
    Dim iDimes as Long
    Dim iNickels as Long
    Dim iPennies as Long

    'assign values to variables
    iPennies = CLng(Val(InputBox("Enter the number of pennies", "Number of Pennies")))
    'perform calculations
    iDollars = Int(iPennies / 100)
    iQuarters = Int((iPennies - (iDollars * 100)) / 25)
    iDimes = Int((iPennies - (iQuarters * 25) - (iDollars * 100)) / 10)
    iNickels = Int((iPennies - (iDimes * 10) - (iQuarters * 25) - (iDollars * 100)) / 5)
    iPennies = Int(iPennies - (iNickels * 5) - (iDimes * 10) - (iQuarters * 25) - (iDollars * 100))
    lblNDollars = "Dollars: " & iDollars
    lblNQuarters = "Quarters: " & iQuarters
    lblNDimes = "Dimes: " & iDimes
    lblNNickels = "Nickels: " & iNickels
    lblNPennies = "Pennies: " & iPennies
    End Sub




    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

  3. #3
    Join Date
    Oct 1999
    Posts
    25

    Re: still can't calculate

    Your problem is not with the val function, it's with your actual calculations. VB does not follow the MDAS (mult, div, add, subtract) convention as a language such as Fortran would. You need to change this part of your code to:
    'perform calculations
    sngDollars = CSng(intPennies) / 100
    sngQuarters = (CSng(lblNdollars.Caption) - CSng(intPennies))/ 25
    sngDimes = (CSng(lblNquarters.Caption) - CSng(intPennies))/ 10
    sngNickels = (CSng(lblNdimes.Caption) - CSng(intPennies)) / 5
    sngNPennies = CSng(intPennies) / 1 ' why?

    Sky1000


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

    Re: still can't calculate

    Thanks for the help! You're a lifesaver!

    kazooie21

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