Question:

Visual Basic .NET problems

by  |  earlier

0 LIKES UnLike

I am having a problem. Im trying to write a simple code for a class project. The code is suppose to convert x number of pennies in a jar into dollars quarters nickles dimes and pennies. so using the example number 2399, that would be 23 dollars 3 quarters 2 dimes and 4 pennies.

Private Sub xCalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xCalcButton.Click

'Converts the total number of pennies into dollars, quarters, dimes, nickels, and pennies

Dim Dollars As Integer

Dim Quarters As Integer

Dim Dimes As Integer

Dim Nickels As Integer

Dim Pennies As Integer

Dim QuarterChange As Integer

Dim DimeChange As Integer

Dim NickelChange As Integer

Dim PenniesChange As Integer

'Divides the number of pennies by 100

Dollars = Val(Me.xPenniesNum.Text) / 100

Me.xDollarLable.Text = Dollars

QuarterChange = Me.xPenniesNum.Text - (Dollars * 100)

Quarters = QuarterChange / 25

Me.xQuartersLable.Text = Quarters

DimeChange = Me.xPenniesNum.Text - (Dollars * 100) - (Quarters)

Dimes = DimeChange / 10

Me.xDimeLable.Text = Dimes

NickelChange = Me.xPenniesNum.Text - (Dollars * 100) - (Quarters) - (Dimes)

Nickels = NickelChange / 5

Me.xNickelLabe.Text = Nickels

PenniesChange = Me.xPenniesNum.Text - (Dollars * 100) - (Quarters) - (Dimes) - (Nickels)

Pennies = PenniesChange / 1

Me.xPenniesLabel.Text = Pennies

End Sub

that is the code i came up with but it doesnt work. I know why it doesnt work and that is when you divide the number by 100 it rounds up to 24 dollars. I just cant figure out how to fix this. Can someone give me a reference or something where i can find out how to write this?

 Tags:

   Report

4 ANSWERS


  1. Try this: Cut and Paste into code pane....

    You may have to adjust the spacing of your labels if you place them horizontally. Don't worry about it if they are vertical.

    Public Class Form1

        Private Sub xCalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xCalcButton.Click

            'Converts the total number of pennies into dollars, quarters, dimes, nickels, and pennies

            Dim StartPennies As Integer

            Dim Dollars, Quarters, Dimes, Nickels, Pennies As Decimal

            StartPennies = Me.xPenniesNum.Text

            'Get dollar value

            Dollars = (Me.xPenniesNum.Text \ 100) 'use \ operator for division, remainder truncated

            Me.xDollarLable.Text = Dollars & " Dollars" 'equals exact number of times

            Me.xPenniesNum.Text = StartPennies - (Dollars * 100) 'Get the remainder in pennies

            Quarters = (Me.xPenniesNum.Text \ 25) 'use \ operator for division, remainder truncated

            Me.xQuartersLable.Text = Quarters & " Quarters" 'equals exact number of times

            Me.xPenniesNum.Text = Me.xPenniesNum.Text - (Quarters * 25) 'Get the remainder in pennies

            Dimes = (Me.xPenniesNum.Text \ 10) 'use \ operator for division, remainder truncated

            Me.xDimeLable.Text = Dimes & " Dimes" 'equals exact number of times

            Me.xPenniesNum.Text = Me.xPenniesNum.Text - (Dimes * 10) 'Get the remainder in pennies

            Nickels = (Me.xPenniesNum.Text \ 5) 'use \ operator for division, remainder truncated

            Me.xNickelLabe.Text = Nickels & " Nickles"  'equals exact number of times

            Me.xPenniesNum.Text = Me.xPenniesNum.Text - (Nickels * 5) 'Get the remainder in pennies

            Pennies = Me.xPenniesNum.Text

            Me.xPenniesLabel.Text = Pennies & " Pennies"

            Me.xPenniesNum.Text = Me.xPenniesNum.Text - (Pennies)

        End Sub

    End Class

    : )


  2. Replace all the variables of the "Integer" type with variables of the "Double" type, meaning that instead of doing this:

    Dim Nickels As Integer

    You would do this:

    Dim Nickels as Double

    Good Luck!

  3. Try using a different variable type in replace of integer.

  4. I would use Decimal instead of integers

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.