none
Problem total due RRS feed

  • Question

  • Hey guys,

    I'm working on an assignment and I can't get the total due to output correctly.The tax is supposed to be 4.5%. No matter what radio button I choose, the subtotal and total due is the same. It will not add the sales tax into the total due.. I would highly appreciate it if someone can double check this for me.Clearly I'm doing something wrong and would like someone to double check my code..

    Public Class Form1
    
        Function doughnut() As Double
    
            Dim x As Double
    
            If Radio_glazed.Checked = True Then
                x = 0.75
            ElseIf Radio_sugar.Checked = True Then
                x = 0.75
            ElseIf Radio_choc.Checked = True Then
                x = 0.75
            ElseIf Radio_filled.Checked = True Then
                x = 0.95
            End If
            Return x
        End Function
    
        Function coffee() As Double
    
            Dim y As Double
    
            '' chooses which ever radio button is checked
    
            If Radio_regular.Checked = True Then
    
                y = 1.5
    
            ElseIf Radio_capp.Checked = True Then
    
                y = 2.75
    
            ElseIf Radio_none.Checked = True Then
    
                y = 0
    
            End If
    
            Return y
    
        End Function
    
        '' function to calculate tax on subtotal and return tax
    
        Function calc_tax(ByVal z As Double) As Double
    
            Return z * (4.5 / 100)
    
        End Function
    
        '' calculate button click event handler
    
        Private Sub btn_calc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_calc.Click
    
            Dim doughnut_price As Double = 0
    
            Dim coffee_price As Double = 0
    
            Dim sub_total, tax, total As Double
    
    
    
            coffee_price = coffee()
    
            doughnut_price = doughnut()
    
            sub_total = doughnut_price + coffee_price
    
            total = sub_total + tax
    
            tax = calc_tax(sub_total)
    
            txt_sub.Text = "$" + sub_total.ToString("#.##")
    
            txt_tax.Text = "$" + tax.ToString("#.##")
    
            txt_total.Text = "$" + total.ToString("#.##")
    
        End Sub
    
        '' exit button click event handler
    
        Private Sub btn_exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_exit.Click
    
            Application.Exit()
    
        End Sub
    
    End Class
    

    Saturday, February 17, 2018 11:35 PM

Answers

All replies

  • The tax is supposed to be 4.5%. No matter what radio button I choose, the subtotal and total due is the same.

    You should calculate the tax amount before you add it to the total.  See:
    https://vbdotnetblog.wordpress.com/overview/debugging/

    • Marked as answer by teck_58 Sunday, February 18, 2018 12:34 AM
    Saturday, February 17, 2018 11:38 PM
  • I understand that, but I don't think I'm doing it right. I clicked on the link, but it just talks about debugging and not how to get it to work.  Is there something wrong with the following code that isn't allowing me to calculate the tax with the subtotal and get the result I need? 

    sub_total = doughnut_price + coffee_price
    
            total = sub_total + tax
    
            tax = calc_tax(sub_total)
        '' function to calculate tax on subtotal and return tax
    
        Function calc_tax(ByVal z As Double) As Double
    
            Return z * (4.5 / 100)
    
        End Function

    Saturday, February 17, 2018 11:47 PM
  • Hi

    I haven't looked through all your code, only this last snippet. It may be that you are getting the Tax AFTER trying to add it to the subtotal.

    Maybe this:

    total = sub_total + calc_tax(sub_total)
    
    
    and remove the Tax = line .........


    Regards Les, Livingston, Scotland

    Saturday, February 17, 2018 11:52 PM
  • I understand that, but I don't think I'm doing it right. I clicked on the link, but it just talks about debugging and not how to get it to work.

    If you debug your program using a breakpoint and single stepping, you will be able to see the problem.  You are adding the tax to the total before the tax gets calculated.  The method you have posted is executed when it is called from your code.  Where the method appears in your code is not relevant.  It only executes when it is called.

    Saturday, February 17, 2018 11:54 PM
  • Hey thanks for your response. I tried this out and now I see a difference in the total due.. but now my sales tax doesn't display. strange

    What exactly was the change you made?

    When you debug your program using single stepping and watch variables, can you see the tax amount being caclualted?  Can you see it being assigned to a variable?  Can you see the text property of the control getting updated?

    Sunday, February 18, 2018 12:10 AM
  • Hey thanks for your response. I tried this out and now I see a difference in the total due.. but now my sales tax doesn't display. strange
    Sunday, February 18, 2018 12:14 AM
  • Just look closely at the code. In particular, look at:

    total = sub_total + tax
    tax = calc_tax(sub_total)

    Just look at that closely.

    Acamar is trying to get you to learn how to debug your program. You must learn that. You would find the problem in your code if you use the debugger. You are right that the debugger won't tell you how to fix it but for this it will tell you the problem, you don't need us to help you.



    Sam Hobbs
    SimpleSamples.Info

    Sunday, February 18, 2018 12:16 AM
  • Man I am so slow when it comes to learning how to use the debugger. And my apologies, that response was for leshay. I removed the tax = line and added total = sub_total + calc_tax(sub_total). Let me figure out how to check for the problem with debugging by what you recommended, and I will let you know what I find out.
    Sunday, February 18, 2018 12:20 AM
  • When I start the debugger, it does not give me any errors in my codes, especially the two lines you pointed out. 
    Sunday, February 18, 2018 12:22 AM
  • Man I am so slow when it comes to learning how to use the debugger. And my apologies, that response was for leshay. I removed the tax = line and added total = sub_total + calc_tax(sub_total). Let me figure out how to check for the problem with debugging by what you recommended, and I will let you know what I find out.

    Hi

    If you are using the variable Tax again somewhere else, then a better way would be to move your original line Tax = ... back up above the SubTotal line and re-use the +Tax as it was. The point of all of this is that you need to understand that calculations etc depend upon the values being 'ready' for use -your original code was trying to use the Tax value BEFORE it had been calculated.


    Regards Les, Livingston, Scotland

    Sunday, February 18, 2018 12:23 AM
  • When I start the debugger, it does not give me any errors in my codes, especially the two lines you pointed out. 

    Showing you the syntax errrors is part of the IDE, not the debugger.  The debugger shows you what the code is actually doing, so you can see why it is not doing what you expect it to do.

    You can't just take that is provided to you and plug it into the program - you must ensure that you understand what the code is designed to change in your program.  In this case you have made more changes than you intended.  Go back to the original code.  Use the debugger if it helps, but one way or another yo have to confirm that you can see that you code will (in sequence):

    Calculate the tax amount.
    Assign the calculated amount to a variable.
    Update the text property of the textbox control with the value of that variable.

    Sunday, February 18, 2018 12:47 AM
  • To add to what Acamar said, before debugging; put a breakpoint at the beginning of btn_calc_Click. You can do that in one of many ways including one of the following (with the cursor on the relevant line):

    • the menu: "Debug" | "Toggle Breakpoint"
    • F9
    • click in the margin at the far-right of the statement (the cusor is not relevant then)

    You will see a Red circle for lines with a breakpoint; the color might be different but probably not.

    Then debug and when execution gets to the breakpoint the debugger will stop execution and take you to the relevant line and wait. Then you can look at things; I won't explain that, I hope you can figure that out. When debugging you can use "Step Into (F11)", "Step Over (F10)" or "Step Out".



    Sam Hobbs
    SimpleSamples.Info

    Sunday, February 18, 2018 1:00 AM