Click to See Complete Forum and Search --> : still can't calculate


kazooie21
October 20th, 1999, 01:58 PM
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

Aaron Young
October 20th, 1999, 02:51 PM
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
adyoung@win.bright.net
aarony@redwingsoftware.com

Sky1000
October 20th, 1999, 02:57 PM
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

kazooie21
October 20th, 1999, 05:38 PM
Thanks for the help! You're a lifesaver!

kazooie21