Results 1 to 2 of 2

Thread: Experts ..... I need help please!!!

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    15

    Post Experts ..... I need help please!!!

    Hello everyone.

    I have a program that is driving me crazy. It is a cashier change return where the user enter the amount owed and the amount paid and when he/she press calculate the program should display the change due as well as the number of Dollars, Quarters, Dimes, Nickels, and pennies returned to the customer. When I run the program sometimes it gives me the right output and others it gives wrong output and it rounds the output. I just want to let you know that I have the option strict on. I will paste the code below please help me ASAP!!!!!!

    Thanks in advance
    vb.net Code:
    1. Option Explicit On
    2. Option Strict On
    3. Option Infer Off
    4.  
    5. Public Class frmMain
    6.  
    7.     Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    8.         ' close the application
    9.         Me.Close()
    10.     End Sub
    11.  
    12.     Private Sub btnClr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClr.Click
    13.  
    14.         ' clear all the labels and textboxes and prepare the screen for the next entry
    15.         txtOwed.Text = ""
    16.         txtPaid.Text = ""
    17.         lblChangDue.Text = ""
    18.         lblDollars.Text = ""
    19.         lblQuart.Text = ""
    20.         lblDimes.Text = ""
    21.         lblNickels.Text = ""
    22.         lblPenn.Text = ""
    23.  
    24.         ' send the focus to the Amount owed text box
    25.         txtOwed.Focus()
    26.  
    27.     End Sub
    28.  
    29.     Private Sub btnCal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCal.Click
    30.  
    31.         ' declare named constants
    32.         Const intDOLLARS_VALUE As Integer = 1
    33.         Const decQUART_VALUE As Decimal = 0.25D
    34.         Const decDIMES_VALUE As Decimal = 0.1D
    35.         Const decNICKELS_VALUE As Decimal = 0.05D
    36.         Const decPENN_VALUE As Decimal = 0.01D
    37.  
    38.         'declare variables
    39.         Dim decOwed As Decimal
    40.         Dim decPaid As Decimal
    41.         Dim decChange As Decimal
    42.         Dim intDollars As Integer
    43.         Dim decRemaining As Decimal
    44.         Dim intQuart As Integer
    45.         Dim decRemainingTwo As Decimal
    46.         Dim intDimes As Integer
    47.         Dim decRemainingThree As Decimal
    48.         Dim intNickels As Integer
    49.         Dim decRemainingFour As Decimal
    50.         Dim intPenn As Integer
    51.  
    52.         ' calculate the change due
    53.         Decimal.TryParse(txtOwed.Text, decOwed)
    54.         Decimal.TryParse(txtPaid.Text, decPaid)
    55.         decChange = decPaid - decOwed
    56.  
    57.         ' claculate the dollars
    58.         intDollars = Convert.ToInt32((decChange / intDOLLARS_VALUE) - (decChange Mod intDOLLARS_VALUE))
    59.         decRemaining = decChange - (intDollars * intDOLLARS_VALUE)
    60.  
    61.         ' calculates the quarters
    62.         intQuart = Convert.ToInt32((decRemaining / decQUART_VALUE) - (decRemaining Mod decQUART_VALUE))
    63.         decRemainingTwo = decRemaining - (intQuart * decQUART_VALUE)
    64.  
    65.         ' calculate the dimes
    66.         intDimes = Convert.ToInt32((decRemainingTwo / decDIMES_VALUE) - (decRemainingTwo Mod decDIMES_VALUE))
    67.         decRemainingThree = decRemainingTwo - (intDimes * decDIMES_VALUE)
    68.  
    69.         ' calculate the nickels
    70.         intNickels = Convert.ToInt32((decRemainingThree / decNICKELS_VALUE) - (decRemainingThree Mod decNICKELS_VALUE))
    71.         decRemainingFour = decRemainingThree - (intNickels * decNICKELS_VALUE)
    72.  
    73.         ' calculate the pennies
    74.         intPenn = Convert.ToInt32((decRemainingFour / decPENN_VALUE) - (decRemainingFour Mod decPENN_VALUE))
    75.  
    76.         lblChangDue.Text = decChange.ToString("C2")
    77.         lblDollars.Text = Convert.ToString(intDollars)
    78.         lblQuart.Text = Convert.ToString(intQuart)
    79.         lblDimes.Text = Convert.ToString(intDimes)
    80.         lblNickels.Text = Convert.ToString(intNickels)
    81.         lblPenn.Text = Convert.ToString(intPenn)
    82.     End Sub
    83.  
    84.     Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    85.         ' gets the username
    86.  
    87.         ' declare a variable
    88.         Dim strUserName As String
    89.  
    90.         Const strPrompt As String = "Enter username:"
    91.         Const strTITLE As String = "Username Entery"
    92.  
    93.         ' assign the username to the variable
    94.         strUserName = InputBox(strPrompt, strTITLE)
    95.  
    96.         ' puts the username beside the Change Calculator
    97.         Me.Text = Me.Text & "  " & strUserName
    98.  
    99.     End Sub
    100.  
    101.     Private Sub CLEARLABELS(ByVal sender As Object, ByVal e As System.EventArgs) _
    102.         Handles txtOwed.TextChanged, txtPaid.TextChanged
    103.  
    104.         ' clears all labels when amount owed or amount paid is changed
    105.         lblChangDue.Text = ""
    106.         lblDollars.Text = ""
    107.         lblQuart.Text = ""
    108.         lblDimes.Text = ""
    109.         lblNickels.Text = ""
    110.         lblPenn.Text = ""
    111.  
    112.     End Sub
    113. End Class
    Last edited by Hack; Sep 8th, 2011 at 11:40 AM. Reason: Added Highlight Tags

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Experts ..... I need help please!!!

    You should convert both the amount_paid and the amount_owned to pennies and do all your calculations in pennies (integers). Here is the rough pseudocode:
    Code:
    penniesPaid = CInt(amount_paid * 100)
    penniesOwned = CInt(amount_owned * 100)
    penniesChange = penniesPaid - penniesOwned
    If penniesChange < 0 Then
       MessageBox.Show("Not enough money tendered. You need to pay more.")
    Else
       'Do the calculations here
       'For dollars
       dollars = penniesChange \ 100
       penniesChange = penniesChange Mod 100
       
       'For quarters
       quarters = penniesChange \ 25
       penniesChange = penniesChange Mod 25
    
       'and so on for dimes, nickels, pennies....
       'You see the pattern. You do integer division to get the count of a unit, then you update the amount
       'left by doing modulus division
       
    End If
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

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