Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

A VB.NET Newbie Problem ...

TimCerejaTimCereja Posts: 17Member
The problem is: A program that outputs the correct number of quarters, dimes, nickels, and pennies for an input of less than one dollar, and paid for with exactly one dollar. It's a trailer record logic program and is supposed to repeat until the sentinel value of 0 is entered. I've written this code and the output is sometimes correct, but is sometimes off by one cent. I'm also not sure how to get the program to repeat, as this only goes through it once. Any help or suggestions would be very appreciated. Thanks ...

Module cisp300asgn7

Sub Main()

Dim count1 As Integer = 0
Dim count2 As Integer = 0
Dim count3 As Integer = 0
Dim count4 As Integer = 0
Dim price As Single
Dim chng As Single

Console.Write("What is the Price? ")
price = Console.ReadLine()


If price = 0 Then
Console.WriteLine("No Price to Process!")
Console.ReadLine()

Else
chng = (1 - price)

End If

Do While chng >= 0.25
chng = chng - 0.25
count1 = count1 + 1

Loop

Do While chng >= 0.1
chng = chng - 0.1
count2 = count2 + 1
Loop

Do While chng >= 0.05
chng = chng - 0.05
count3 = count3 + 1
Loop


Do While chng > 0
chng = chng - 0.01
count4 = count4 + 1
Loop

Console.WriteLine("The customer's change is")
Console.WriteLine("Quarter(s) {0}", count1)
Console.WriteLine("Dime(s) {0}", count2)
Console.WriteLine("Nickel(s) {0}", count3)
Console.WriteLine("Penny(s) {0}", count4)


Console.ReadLine()


End Sub

End Module

Comments

  • geruagerua Posts: 1Member
    1.Put a breakpoint on the statement: Do While chng > 0
    2.Begin debugging. The output halts before you enter the loop.
    3.Now use the step-in debugger to go through your loop.
    4.Change your count4 variable to a double or something and check..

    I think I have shown you the solution why you get the extra cent...

    Regarding how to repeat...

    Start the program with a yes/no declaration, say, bRunAgain.
    bRunAgain= yes
    So...

    do while bRunAgain=yes
    ...
    ...run program
    ...
    ...do you wish to continue
    ...if yes, loop
    ...if no, end

    G
  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    You are using the Single variable with Price and Chng. In the past I have run into rounding issues with Single, where constant use of the variable will go "off" 1 or two whole numbers. As the last poster suggested, I would try switching Price and Chng variables to a Double instead of Single. This will probably fix that problem

    As far as making it loop until the user enters 0? I would put the entire block of code into a Do While Loop. Use the If Statement "if price = 0 then" to close execution of the software with the END statement. It would look like this:

    [code]
    Sub Main()
    Dim price As Double = 0
    Dim chng As Double = 0

    Do 'With no while condition, you will loop for ever
    'These are inside so they reset each pass of the loop
    Dim count1 As Integer = 0
    Dim count2 As Integer = 0
    Dim count3 As Integer = 0
    Dim count4 As Integer = 0
    chng = 0 'This needs to reset each pass of the loop

    Console.Write("What is the Price? ")
    price = Console.ReadLine()

    If price = 0 Then
    Console.WriteLine("No Price to Process!")
    Console.Write("Press Enter to Continue. ")
    Console.ReadLine()
    End 'This should end execution of the program
    ElseIf price < 0 Then
    Console.WriteLine("You cannot pay me a negative amount!")
    Console.Write("Press Enter to Continue. ")
    Console.ReadLine()
    Else
    chng = (1 - price)
    End If

    Do While chng >= 0.25
    chng = chng - 0.25
    count1 = count1 + 1
    Loop

    Do While chng >= 0.1
    chng = chng - 0.1
    count2 = count2 + 1
    Loop

    Do While chng >= 0.05
    chng = chng - 0.05
    count3 = count3 + 1
    Loop

    Do While chng > 0
    chng = chng - 0.01
    count4 = count4 + 1
    Loop

    Console.WriteLine("The customer's change is")
    Console.WriteLine("Quarter(s) {0}", count1)
    Console.WriteLine("Dime(s) {0}", count2)
    Console.WriteLine("Nickel(s) {0}", count3)
    Console.WriteLine("Penny(s) {0}", count4)
    Console.Write("Press Enter to Continue. ")
    Console.ReadLine()
    Loop
    End Sub
    [/code]
Sign In or Register to comment.