12 Replies - 3375 Views - Last Post: 17 September 2012 - 01:18 PM Rate Topic: -----

#1 mokazan15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-September 12

Need help with calculating change in VB.

Posted 17 September 2012 - 11:03 AM

I need help with calculating change I keep returning garbage. Can someone please help me solve this!?

 
 'Variables
        Const strPROMPT As String = "Username"
        Const strTITLE As String = "Name Entry"
        Static strUsername As String
        Dim decAmountOwed As Decimal
        Dim decPaid As Decimal
        Dim decChangeDue As Decimal
        Dim intDollar As Integer
        Dim intQuarter As Integer
        Dim intDime As Integer
        Dim intNickel As Integer
        Dim intPennie As Integer

        'assign the name to the class-level variable
        strUsername = InputBox(strPROMPT, strTITLE, strUsername)
        'Sorting Change for Calculation

        Dim intTempValue As Integer

        ' Subtract AmountGiven from the Total

        Dim sngResult As Single
        sngResult = CSng(txtPaid.Text) - CSng(txtAmountOwed.Text)
        lblChangeDue.Text = sngResult.ToString

        ' Convert Change to Dollars
        sngResult = CSng(CSng(lblChangeDue.Text) / 1.0)
        lblDollar.Text = sngResult.ToString
        Math.Floor(1)

        'Calculates quarters owed minus dollars we are going to pay
        intTempValue = intTempValue Mod 100

        'Calc Quarters remaining minus dollars paid
        intQuarter = intTempValue \ 25
        lblQuarters.Text = intQuarter

        'Calculate the number of dimes
        intTempValue = intTempValue Mod 25    'Calc dimes remaining minus quarters paid
        intDime = intTempValue \ 10
        lblDimes.Text = intDime

        'Calculate nickels
        intTempValue = intTempValue Mod 10    'Calc nickels remaining minus dimes paid
        intNickel = intTempValue \ 5
        lblNickels.Text = intNickel

        'Calculate pennies
        intTempValue = intTempValue Mod 5    'Calc pennies remaining minus nickels paid
        intPennie = intTempValue
        lblPennies.Text = intPennie
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Close()
    End Sub

    

    Private Sub btnClearScrn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearScrn.Click
        'clear screen for next order
        txtAmountOwed.Text = String.Empty
        lblChangeDue.Text = String.Empty
        txtPaid.Text = String.Empty
        lblDollar.Text = String.Empty
        lblDime.Text = String.Empty
        lblNickel.Text = String.Empty
        lblPennie.Text = String.Empty
        ' sned the focus to the name text box
        txtAmountOwed.Focus()
    End Sub

    Private Sub ClearLabels(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmountOwed.TextChanged, txtPaid.TextChanged
        lblChangeDue.Text = String.Empty
        lblDollar.Text = String.Empty
        lblDime.Text = String.Empty
        lblNickel.Text = String.Empty
        lblPennie.Text = String.Empty
    End Sub




Is This A Good Question/Topic? 0
  • +

Replies To: Need help with calculating change in VB.

#2 jgferguson  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 31
  • Joined: 11-July 12

Re: Need help with calculating change in VB.

Posted 17 September 2012 - 11:30 AM

Hi,

We will need to know more information about the problem you are having: at what point does the routine return 'garbage'? Does the variable sngResult give you what you expect and there is a problem with converting to the various sub levels of currency (not being American I am not familiar with the relationship between dollars, nickels, dimes etc.).

Put a breakpoint in your code and run it again and check the variable results a line at a time and then you should be able to determine where the programme is not performing as expected. In addition you can assign a Watch to each variable so that you can see at what stage the values change and then work out what is happening to cause the change.

Sorry I can't be of more help but without knowing what the 'garbage' is we can only speculate as to the problem is.

Cheers,
Jerry.
Was This Post Helpful? 0
  • +
  • -

#3 mokazan15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-September 12

Re: Need help with calculating change in VB.

Posted 17 September 2012 - 11:40 AM

[quote name='jgferguson' date='17 September 2012 - 11:30 AM' timestamp='1347906635' post='1703948']
Hello Jerry,

The garbage I am referring to is the output is displaying random numbers each time. Nickels= 5 cents Dimes =10 cents Quarter = 25 cents and Penny= 1 cent

I am trying to write a program that asks the user to enter an amount of money and display the exact change( display how many dollars quarters dimes nickels and pennies are returned) as the output if there are any.
Was This Post Helpful? 0
  • +
  • -

#4 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 455
  • View blog
  • Posts: 3,144
  • Joined: 12-January 10

Re: Need help with calculating change in VB.

Posted 17 September 2012 - 11:50 AM

example of what are you returning?

This post has been edited by DarenR: 17 September 2012 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

#5 mokazan15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-September 12

Re: Need help with calculating change in VB.

Posted 17 September 2012 - 11:54 AM

View PostDarenR, on 17 September 2012 - 11:50 AM, said:

example of what are you returning?


An example would be...

Amount Owed: 25.35
Amount Paid: 30.00
Change Due : .65

Dollars:0
Nickels:1
Quarters:2
Dimes: 1
Pennies:0 <-------I need to display the exact change returned

View Postmokazan15, on 17 September 2012 - 11:54 AM, said:

View PostDarenR, on 17 September 2012 - 11:50 AM, said:

example of what are you returning?


An example would be...

Amount Owed: 25.35
Amount Paid: 30.00
Change Due : .65

Dollars:5
Nickels:1
Quarters:2
Dimes: 1
Pennies:0 <-------I need to display the exact change returned

Was This Post Helpful? 0
  • +
  • -

#6 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 455
  • View blog
  • Posts: 3,144
  • Joined: 12-January 10

Re: Need help with calculating change in VB.

Posted 17 September 2012 - 11:58 AM

so are you saying your pennies arent working?
Was This Post Helpful? 0
  • +
  • -

#7 jgferguson  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 31
  • Joined: 11-July 12

Re: Need help with calculating change in VB.

Posted 17 September 2012 - 11:58 AM

Hi,

An example would be...

Amount Owed: 25.35
Amount Paid: 30.00
Change Due : .65

Surely the change will be 4.65! To me the error is in your calculation of the result.

The conversion of 0.65 to quarters, etc seems to be correct.

Jerry.
Was This Post Helpful? 0
  • +
  • -

#8 mokazan15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-September 12

Re: Need help with calculating change in VB.

Posted 17 September 2012 - 12:02 PM

View PostDarenR, on 17 September 2012 - 11:58 AM, said:

so are you saying your pennies arent working?



My main problem is my output would be

amount due:5.00
amount paid:10
change due: 5.00

dollars: -9
nickels: 45
pennies: 10
dimes: 34
There are random numbers each time as my output i need help fixing the error.
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,462
  • Joined: 29-May 08

Re: Need help with calculating change in VB.

Posted 17 September 2012 - 12:04 PM

Have you tried debugging your code? By stepping through the code and see it values of the variable match what you expect them to be.
Was This Post Helpful? 0
  • +
  • -

#10 mokazan15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-September 12

Re: Need help with calculating change in VB.

Posted 17 September 2012 - 12:11 PM

View PostAdamSpeight2008, on 17 September 2012 - 12:04 PM, said:

Have you tried debugging your code? By stepping through the code and see it values of the variable match what you expect them to be.



I debugged until i realized my conversion was wrong and I don't know any other way to convert
this is a sample of my outputAttached Image
Was This Post Helpful? 0
  • +
  • -

#11 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 455
  • View blog
  • Posts: 3,144
  • Joined: 12-January 10

Re: Need help with calculating change in VB.

Posted 17 September 2012 - 01:01 PM

lblChangeDue.Text = Math.Round(sngResult,2).ToString 

Was This Post Helpful? 0
  • +
  • -

#12 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 267
  • View blog
  • Posts: 1,476
  • Joined: 07-April 08

Re: Need help with calculating change in VB.

Posted 17 September 2012 - 01:13 PM

you may be getting the weird results because you are dividing the change due by 1.0 instead multiplying it by 100. If my change was 4.65 and you divide that by 1.0 you still get 4.65, but if you multiply it by 100 (This tells you how many pennies it is so that your other calculations will work) you would get 465.

Try changing it to

sngResult = CSng(CSng(lblChangeDue.Text) * 100)


Was This Post Helpful? 0
  • +
  • -

#13 mokazan15  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-September 12

Re: Need help with calculating change in VB.

Posted 17 September 2012 - 01:18 PM

View Postrgfirefly24, on 17 September 2012 - 01:13 PM, said:

you may be getting the weird results because you are dividing the change due by 1.0 instead multiplying it by 100. If my change was 4.65 and you divide that by 1.0 you still get 4.65, but if you multiply it by 100 (This tells you how many pennies it is so that your other calculations will work) you would get 465.

Try changing it to

sngResult = CSng(CSng(lblChangeDue.Text) * 100)




Your code helped thank you. All i have to do is focus on the rest of the change

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1