CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2016
    Posts
    2

    [RESOLVED] Radio Button Math Wrong???

    Code:
       Private Sub CalculateToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CalculateToolStripMenuItem.Click
            'declare all constants and variables
            Dim coupon As Decimal = 0.1 'discount codes
            Dim customer As Decimal = 0.15 'discount codes
            Dim nodisc As Decimal = 0
            Dim coupcode As Decimal
            Dim toppingtotal As Decimal
            Dim sales As Decimal = 0.08 'sales tax
            Dim pepp As Decimal = 0.75 'each topping seperate to simplify adding, for me
            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}} 'declare the array
            'this else/if is to output errors in case customer information boxes are empty
            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 = nodisc
            ElseIf tennoff.Checked Then
                coupcode = coupon
            ElseIf prefcus.Checked Then
                coupcode = customer
            End If
    
            If peppers.Checked = True Then 'adds peppers to cost if applicable
                toppingtotal += pepp
            End If
    
            If onions.Checked = True Then 'adds onions to cost if applicable
                toppingtotal += onoin
            End If
    
            If xcheese.Checked = True Then 'adds extra cheese to cost if applicable
                toppingtotal += cheese
            End If
    
            If sizebox.SelectedIndex = -1 Then  'error code if no size is chosen
                MessageBox.Show("Please choose a pizza size.")
                Exit Sub 'exits instead of continuing calculation
            End If
    
            If crustbox.SelectedIndex = -1 Then 'error code if no crust is chosen
                MessageBox.Show("Please choose a crust style.")
                Exit Sub 'exits instead of continuing calculation
            End If
    
            If sizebox.Text = "Small" Then  'if else to associate cost array with what crust and size pizza
                If crustbox.Text = "Regular" Then 'small regular
                    pizzabox.Text = pizzaArrayDecimal(0, 0)
                    dapizza = pizzaArrayDecimal(0, 0)
                ElseIf crustbox.Text = "Stuffed Crust" Then 'small stuffed crust
                    pizzabox.Text = pizzaArrayDecimal(1, 0)
                    dapizza = pizzaArrayDecimal(1, 0)
                End If
            ElseIf sizebox.Text = "Medium" Then
                If crustbox.Text = "Regular" Then 'medium regular
                    pizzabox.Text = pizzaArrayDecimal(1, 0)
                    dapizza = pizzaArrayDecimal(1, 0)
                ElseIf crustbox.Text = "Stuffed Crust" Then 'medium stuffed crust
                    pizzabox.Text = pizzaArrayDecimal(1, 1)
                    dapizza = pizzaArrayDecimal(1, 1)
                End If
            ElseIf sizebox.Text = "Large" Then
                If crustbox.Text = "Regular" Then 'large regular
                    pizzabox.Text = pizzaArrayDecimal(0, 2)
                    dapizza = pizzaArrayDecimal(0, 2)
                ElseIf crustbox.Text = "Stuffed Crust" Then 'large stuffed crust
                    pizzabox.Text = pizzaArrayDecimal(1, 2)
                    dapizza = pizzaArrayDecimal(1, 2)
                End If
            ElseIf sizebox.Text = "Monster" Then
                If crustbox.Text = "Regular" Then 'monster regular
                    pizzabox.Text = pizzaArrayDecimal(0, 3)
                    dapizza = pizzaArrayDecimal(0, 3) '
                ElseIf crustbox.Text = "Stuffed Crust" Then 'monster stuffed crust
                    pizzabox.Text = pizzaArrayDecimal(1, 3)
                    dapizza = pizzaArrayDecimal(1, 3)
                End If
            End If
    
            discountbox.Text = String.Format("{0:n2}", couptotal) 'output discount 
            toppingsbox.Text = String.Format("{0:n2}", toppingtotal) 'output toppings total
            couptotal = (toppingtotal + dapizza) * coupcode 'calculate discount
            total = (toppingtotal + dapizza - couptotal) * sales 'calculate sales tax total
            fintotal = toppingtotal + dapizza - couptotal + total 'calculate final total
            totalbox.Text = String.Format("{0:n2}", fintotal) 'output final total
            taxbox.Text = String.Format("{0:n2}", total) 'output sales tax 
        End Sub
    So, I have the code above. The problem I'm having is calculating the discount using radio buttons none, prefcus, and tennoff. If one is chosen, it will default to one of the constant discount options defined in my variables, then used to calculate and output at the bottom of my code. No matter what I choose, it outputs the discount to 0.00. The code works great except for this and has everything I need it to have, this is only the calculate as this is the only part that refers to the radio buttons and outputs.

    Any help would be much appreciated. I've redone the radio button parts countless times, and stared at it for at least an hour, to no avail.

  2. #2
    Join Date
    Apr 2016
    Posts
    2

    Re: [RESOLVED] Radio Button Math Wrong???

    If anyone has this problem in the future, it is because the calculation is before the discount output. The math was showing up correctly, but the discount was outputting a 0.00 because I sent it to the textbox before calculating it.

Tags for this Thread

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

Featured