Results 1 to 4 of 4

Thread: Help with program vb.net

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2003
    Posts
    3

    Help with program vb.net

    This is my program for school, I am new to vb.net and cannot get this to work for the life of me, can anyone help me. It is suppose to show how many quarters, dimes, nickels and pennies to hand back as change. Thank you in advance.
    Module change1

    Sub Main()
    'declare variables
    Dim change, price, num_qtrs, num_dimes, num_nickels, num_pennies, quarters, dimes, nickels, pennies As Integer

    'populate variables
    num_qtrs = 100 \ 25
    num_dimes = 100 \ 10
    num_nickels = 100 \ 5
    num_pennies = 100 \ 1


    Console.WriteLine("Welcome to the Change Calculator")
    Console.WriteLine("You enter a price and I'll calculate the change")
    Console.WriteLine("Are you ready to begin?")

    Do While Console.ReadLine().ToUpper.Equals("YES")

    'Prompt the user to enter a price
    Console.WriteLine("Enter the price:")

    ' divide and dispay results
    price = Convert.ToInt32(Console.ReadLine())
    change = 100 - price

    Console.WriteLine("change " & +change)
    Console.WriteLine("quarters " & +quarters)
    Console.WriteLine("dimes " & +dimes)
    Console.WriteLine("nickels " & +nickels)
    Console.WriteLine("pennies " & +pennies)

    Console.WriteLine("Would you like to make more change?")

    Console.WriteLine("GoodBye")


    Loop
    End Sub

    End Module

  2. #2
    Lively Member
    Join Date
    Jul 2003
    Location
    Kuala Lumpur (Malaysia)
    Posts
    92

    Smile

    Hi,

    You are not assign the value to the variable you display, so it will display "0" all the way.... Beside that, what actually you want to show.

  3. #3
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    This should work....

    If you have any questions, ask...

    EDIT: forgot a error handler...

    VB Code:
    1. Module modChange
    2.  
    3.     Sub Main()
    4.         'Setup Change Bin
    5.  
    6.         Dim CurrentChange As sChange ' This is what the container has before starting
    7.  
    8.         With CurrentChange
    9.             .sQuarters = 100
    10.             .sDimes = 100
    11.             .sNickles = 100
    12.             .sPennies = 100
    13.         End With
    14.  
    15.         'I Better Introduce myself...
    16.  
    17.         Console.WriteLine("Welcome to the Change Calculator")
    18.         Console.WriteLine("You enter a price and I'll calculate the change")
    19.  
    20.         'leave 3 Lines
    21.         Console.WriteLine("")
    22.         Console.WriteLine("")
    23.         Console.WriteLine("")
    24.  
    25.         Console.WriteLine("Are you ready to begin?")
    26.  
    27.         'Lets get this party started....
    28.  
    29.         Do While Console.ReadLine().ToUpper.Equals("YES")
    30.  
    31.             Console.WriteLine("Please Type the price: ")
    32.  
    33.             Dim decCost As Decimal
    34.  
    35.             Try
    36.  
    37.                 decCost = CDec(Console.ReadLine())
    38.  
    39.             Catch ex As InvalidCastException
    40.                 Console.WriteLine("Invaild Number!")
    41.             End Try
    42.  
    43.  
    44.             Dim NewChange As sChange ' This is what the container has after starting (is is passed byref)
    45.             Dim ClientsChange As sChange ' this is what the result is
    46.  
    47.             'Get The change
    48.             ClientsChange = CalculateChange(CurrentChange, decCost, NewChange)
    49.  
    50.             'Display some info the the user
    51.  
    52.             'leave 3 Lines
    53.             Console.WriteLine("")
    54.             Console.WriteLine("")
    55.             Console.WriteLine("")
    56.  
    57.             Console.WriteLine("Calculation Results....")
    58.  
    59.             With ClientsChange
    60.  
    61.                 Console.WriteLine("")
    62.  
    63.                 Console.WriteLine("Total Change: $" & .Total)
    64.                 Console.WriteLine("Quarters: " & .sQuarters)
    65.                 Console.WriteLine("Dimes: " & .sDimes)
    66.                 Console.WriteLine("Nickles: " & .sNickles)
    67.                 Console.WriteLine("Pennies: " & .sPennies)
    68.  
    69.             End With
    70.  
    71.             'leave 3 Lines
    72.             Console.WriteLine("")
    73.             Console.WriteLine("")
    74.             Console.WriteLine("")
    75.  
    76.             Console.WriteLine("Whats now in the contaner...")
    77.             With NewChange
    78.  
    79.                 Console.WriteLine("")
    80.  
    81.                 Console.WriteLine("Total Change: $" & .Total)
    82.                 Console.WriteLine("Quarters: " & .sQuarters)
    83.                 Console.WriteLine("Dimes: " & .sDimes)
    84.                 Console.WriteLine("Nickles: " & .sNickles)
    85.                 Console.WriteLine("Pennies: " & .sPennies)
    86.  
    87.             End With
    88.  
    89.             Console.WriteLine("Would you like to make more change?")
    90.  
    91.             'Remove the following line if you dont want the bins capacity to carry over
    92.             CurrentChange = NewChange
    93.  
    94.         Loop
    95.  
    96.         Console.WriteLine("GoodBye")
    97.  
    98.     End Sub
    99.  
    100.     Public Structure sChange
    101.         Dim sQuarters As Short
    102.         Dim sDimes As Short
    103.         Dim sNickles As Short
    104.         Dim sPennies As Short
    105.  
    106.         Function Total() As Decimal
    107.             Dim decTotal As Decimal
    108.  
    109.             decTotal += CDec(sQuarters * 0.25)
    110.             decTotal += CDec(sDimes * 0.1)
    111.             decTotal += CDec(sNickles * 0.05)
    112.             decTotal += CDec(sPennies * 0.01)
    113.  
    114.             Return decTotal
    115.         End Function
    116.     End Structure
    117.  
    118.     Public Function CalculateChange(ByVal CurrentChange As sChange, ByVal decCost As Decimal, ByRef NewChange As sChange) As sChange
    119.         'Calculate the most efficent way to give the client change
    120.  
    121.         Dim ClientsChange As sChange
    122.  
    123.         Dim decCurrentValue As Decimal = decCost
    124.  
    125.         Dim sTmp As Short
    126.  
    127.         'Get the most quarters that we can use...
    128.         sTmp = Fix(decCurrentValue / 0.25)
    129.  
    130.         'Do we have enough quarters?
    131.  
    132.         If sTmp > CurrentChange.sQuarters Then
    133.             'Subtract what we can use
    134.             decCurrentValue -= (CurrentChange.sQuarters * 0.25)
    135.             'update new change
    136.             ClientsChange.sQuarters = CurrentChange.sQuarters
    137.             NewChange.sQuarters = 0
    138.         Else
    139.             decCurrentValue -= (sTmp * 0.25)
    140.             'update new change (remove the quarters we used
    141.             ClientsChange.sQuarters = sTmp
    142.             NewChange.sQuarters = CurrentChange.sQuarters - sTmp
    143.         End If
    144.  
    145.         ':Move on to the next biggest divison, Dimes
    146.  
    147.         'Get the most Dimes that we can use...
    148.         sTmp = Fix(decCurrentValue / 0.1)
    149.  
    150.         'Do we have enough Dimes?
    151.  
    152.         If sTmp > CurrentChange.sDimes Then
    153.             'Subtract what we can use
    154.             decCurrentValue -= (CurrentChange.sDimes * 0.25)
    155.             'update new change
    156.             ClientsChange.sDimes = CurrentChange.sDimes
    157.             NewChange.sDimes = 0
    158.         Else
    159.             decCurrentValue -= (sTmp * 0.1)
    160.             'update new change (remove the Dimes we used
    161.             ClientsChange.sDimes = sTmp
    162.             NewChange.sDimes = CurrentChange.sDimes - sTmp
    163.         End If
    164.  
    165.         ':Move on to the next biggest divison, Nickels
    166.  
    167.         'Get the most Nickles that we can use...
    168.         sTmp = Fix(decCurrentValue / 0.05)
    169.  
    170.         'Do we have enough Nickles?
    171.  
    172.         If sTmp > CurrentChange.sNickles Then
    173.             'Subtract what we can use
    174.             decCurrentValue -= (CurrentChange.sNickles * 0.05)
    175.             'update new change
    176.             ClientsChange.sNickles = CurrentChange.sNickles
    177.             NewChange.sNickles = 0
    178.         Else
    179.             decCurrentValue -= (sTmp * 0.05)
    180.             'update new change (remove the Nickles we used
    181.             ClientsChange.sNickles = sTmp
    182.             NewChange.sNickles = CurrentChange.sNickles - sTmp
    183.         End If
    184.  
    185.         ':Move on to the next, and last, biggest divison, Pennies
    186.  
    187.         'Get the most Pennies that we can use...
    188.         sTmp = Fix(decCurrentValue / 0.01)
    189.  
    190.         'Do we have enough Pennies?
    191.  
    192.         If sTmp > CurrentChange.sPennies Then
    193.             'Subtract what we can use
    194.             decCurrentValue -= (CurrentChange.sPennies * 0.01)
    195.             'update new change
    196.             ClientsChange.sPennies = CurrentChange.sPennies
    197.             NewChange.sPennies = 0
    198.         Else
    199.             decCurrentValue -= (sTmp * 0.01)
    200.             'update new change (remove the Pennies we used
    201.             ClientsChange.sPennies = sTmp
    202.             NewChange.sPennies = CurrentChange.sPennies - sTmp
    203.         End If
    204.  
    205.         'Change calculation complete
    206.  
    207.         'Should check here if we didnt have enough change to give the client
    208.  
    209.         If decCurrentValue <> 0 Then
    210.             'We gotz a problem
    211.             'but you can handle this
    212.             'if you want...
    213.             MsgBox("Error Not Enough Change!")
    214.         End If
    215.  
    216.         Return ClientsChange
    217.  
    218.     End Function
    219.  
    220. End Module
    Last edited by <ABX; Oct 12th, 2003 at 10:44 PM.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  4. #4
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    This is what your looking for.
    Attached Files Attached Files
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

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