Results 1 to 4 of 4

Thread: [RESOLVED] Radio buttons to calculate discount

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    5

    Resolved [RESOLVED] Radio buttons to calculate discount

    So back again with what's probably a simple mistake. I have this program to calculate the cost of pizza, including radio buttons
    "none" "tennoff" and "prefcus". I'm trying to specify discount depending on which radio button is chosen. When I run the program, the discount textbox outputs 0.00 and of course it isn't included in my math. I've tested a few different things, but I still get 0.00 for my discount.
    I included a picture of my program just for reference, in case it helps.

    **Note. I know some of my code is not the most effective way to get the calculations and outputs. I did it a best I understood it.

    Name:  bbbba.jpg
Views: 1853
Size:  22.7 KB
    Code:
        Private Sub CalculateToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CalculateToolStripMenuItem.Click
            Dim coupon As Decimal = 0.1
            Dim customer As Decimal = 0.15
            Dim coupcode As Decimal
            Dim toppingtotal As Decimal
            Dim sales As Decimal = 0.08
            Dim pepp As Decimal = 0.75
            Dim onoin As Decimal = 0.75
            Dim cheese As Decimal = 0.75
            Dim couptotal As Decimal
            Dim dapizza As Decimal
            Dim total As Decimal
            Dim fintotal As Decimal
            Dim pizzaArrayDecimal(,) As Decimal = {{5.99, 6.99, 7.99, 8.99},
                                                   {6.99, 7.99, 8.99, 9.99}}
            If namebox.Text = "" Then
                MessageBox.Show("Error. Missing information.")
            ElseIf streetbox.Text = "" Then
                MessageBox.Show("Error. Missing information.")
            ElseIf citybox.Text = "" Then
                MessageBox.Show("Error. Missing information.")
            ElseIf statebox.Text = "" Then
                MessageBox.Show("Error. Missing information.")
            ElseIf zipbox.Text = "" Then
                MessageBox.Show("Error. Missing information.")
            End If
    
            If none.Checked Then
                coupcode = ""
            ElseIf tennoff.Checked Then
                coupcode = coupon
            ElseIf prefcus.Checked Then
                coupcode = customer
            End If
    
            If peppers.Checked = True Then
                toppingtotal += pepp
            End If
    
            If onions.Checked = True Then
                toppingtotal += onoin
            End If
    
            If xcheese.Checked = True Then
                toppingtotal += cheese
            End If
    
            If sizebox.Text = "Small" Then
                If crustbox.Text = "Regular" Then
                    pizzabox.Text = pizzaArrayDecimal(0, 0)
                    dapizza = pizzaArrayDecimal(0, 0)
                ElseIf crustbox.Text = "Stuffed Crust" Then
                    pizzabox.Text = pizzaArrayDecimal(1, 0)
                    dapizza = pizzaArrayDecimal(1, 0)
                End If
            ElseIf sizebox.Text = "Medium" Then
                If crustbox.Text = "Regular" Then
                    pizzabox.Text = pizzaArrayDecimal(1, 0)
                    dapizza = pizzaArrayDecimal(1, 0)
                ElseIf crustbox.Text = "Stuffed Crust" Then
                    pizzabox.Text = pizzaArrayDecimal(1, 1)
                    dapizza = pizzaArrayDecimal(1, 1)
                End If
            ElseIf sizebox.Text = "Large" Then
                If crustbox.Text = "Regular" Then
                    pizzabox.Text = pizzaArrayDecimal(0, 2)
                    dapizza = pizzaArrayDecimal(0, 2)
                ElseIf crustbox.Text = "Stuffed Crust" Then
                    pizzabox.Text = pizzaArrayDecimal(1, 2)
                    dapizza = pizzaArrayDecimal(1, 2)
                End If
            ElseIf sizebox.Text = "Monster" Then
                If crustbox.Text = "Regular" Then
                    pizzabox.Text = pizzaArrayDecimal(0, 3)
                    dapizza = pizzaArrayDecimal(0, 3)
                ElseIf crustbox.Text = "Stuffed Crust" Then
                    pizzabox.Text = pizzaArrayDecimal(1, 3)
                    dapizza = pizzaArrayDecimal(1, 3)
                End If
            End If
    
            discountbox.Text = String.Format("{0:n2}", couptotal)
            toppingsbox.Text = String.Format("{0:n2}", toppingtotal)
            couptotal = coupcode * (toppingtotal + dapizza)
            total = (toppingtotal + dapizza) * sales
            fintotal = toppingtotal + dapizza - coupcode + total
            totalbox.Text = String.Format("{0:n2}", fintotal)
            taxbox.Text = String.Format("{0:n2}", total)
        End Sub
    
        Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
            Me.Close()
        End Sub
    End Class

  2. #2

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    5

    Re: Radio buttons to calculate discount

    Can anyone help me out? The code is completely finished and working great except for this one problem that I can't seem to fix.

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Radio buttons to calculate discount

    Look at these three lines.

    Code:
            discountbox.Text = String.Format("{0:n2}", couptotal)
            toppingsbox.Text = String.Format("{0:n2}", toppingtotal)
            couptotal = coupcode * (toppingtotal + dapizza)
    You display the coupon discount before you calculate it. So it's 0.00 when you display it, but has a value when you later calculate the total.

    I stick to this order in all of my code that works with the UI:
    • Get everything I need from controls and put it in variables.
    • Do math with the variables.
    • Put the results into UI controls.


    Another tip: you don't have to declare variables at the top of a method. Life is often easier if you wait to declare them until very close to where they are used. You'd have caught this error right away if you tried to write it this way. You'd have either:

    Code:
            discountbox.Text = String.Format("{0:n2}", couptotal) ' ERROR: couptotal not declared
    Or:
    Code:
    Dim couptotal As Decimal
    discountbox.Text = String.Format("{0:n2}", couptotal) ' Oh! Right. This is why it's 0.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2016
    Posts
    5

    Re: Radio buttons to calculate discount

    I knew it was something dumb. I'm so used to what my instructors told me to do in C++ and declare my variables first.

    I realize now not having the calculations before the output and all jumbled in together with the outputs is poor design. When I code, its very all over the place, lol.

    Thanks so much!

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