Public Class Form1

    Private Sub btnCompute_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCompute.Click
        Dim pizza, fries, drinks As Double

        ' inputs by customer
        pizza = CDbl(txtPizza.Text)
        fries = CDbl(txtFries.Text)
        drinks = CDbl(txtDrinks.Text)
        displayBill(pizza, fries, drinks)
    End Sub

    Sub displayBill(ByVal num1 As Double, ByVal num2 As Double, ByVal num3 As Double)
        Dim sum As Double
        Dim calnum1, calnum2, calnum3 As Double
        Dim fmtstr As String = "{0, -15} {1, 17:n} {2,19:c2}"

        'calculation for number of item ordered
        calnum1 = txtPizza.Text + num1
        calnum2 = num2 + txtFries.Text
        calnum3 = num3 + txtDrinks.Text

        'Price of each food item
        num1 = 1.75
        num2 = 2.0
        num3 = 1.25

        sum = calnum1 + calnum2 + calnum3

        'list of products view
        lstResult1.Items.Clear()
        lstResult1.Items.Add(String.Format(fmtstr, "Item: ", "Quantity", "Price"))
        lstResult1.Items.Add(String.Format(fmtstr, "Pizza Slices", txtPizza.Text, num1))
        lstResult1.Items.Add(String.Format(fmtstr, "Fries", txtFries.Text, num2))
        lstResult1.Items.Add(String.Format(fmtstr, "Soft Drinks", txtDrinks.Text, num3))
        lstResult1.Items.Add(String.Format(fmtstr, "", "", ""))
        lstResult1.Items.Add(String.Format(fmtstr, "Total :", "", sum))

    End Sub

End Class

thanks in advance
MOMO

Public Class Form1

    Private Sub btnCompute_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCompute.Click
        Dim pizza, fries, drinks As Double

        ' inputs by customer
        pizza = CDbl(txtPizza.Text)
        fries = CDbl(txtFries.Text)
        drinks = CDbl(txtDrinks.Text)
        displayBill(pizza, fries, drinks)

You are calling the function with the number of items ordered (I think- there's no comments for proper understanding so I can only guess)

End Sub

    Sub displayBill(ByVal num1 As Double, ByVal num2 As Double, ByVal num3 As Double)
        Dim sum As Double
        Dim calnum1, calnum2, calnum3 As Double
        Dim fmtstr As String = "{0, -15} {1, 17:n} {2,19:c2}"

        'calculation for number of item ordered
        calnum1 = txtPizza.Text + num1
        calnum2 = num2 + txtFries.Text
        calnum3 = num3 + txtDrinks.Text

Now you add the number of items ordered to the number of items ordered passed in. So you have twice as many items as entered.

'Price of each food item
        num1 = 1.75
        num2 = 2.0
        num3 = 1.25

Now you wipe out the values passed into the function.

Hope that helps.

Hi mjltech84,

Try

Public Class Form1
 
    Private Sub btnCompute_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCompute.Click
        Dim pizza, fries, drinks As Double
 
        ' inputs by customer
        pizza = CDbl(txtPizza.Text)
        fries = CDbl(txtFries.Text)
        drinks = CDbl(txtDrinks.Text)
        displayBill(pizza, fries, drinks)
    End Sub
 
    Sub displayBill(ByVal num1 As Double, ByVal num2 As Double, ByVal num3 As Double)
        Dim sum As Double
        Dim calnum1, calnum2, calnum3 As Double
        Dim fmtstr As String = "{0, -15} {1, 17:n} {2,19:c2}"
 
        'Price of each food item
        num1 = 1.75
        num2 = 2.0
        num3 = 1.25

        'calculation for number of item ordered
        calnum1 = txtPizza.Text + num1
        calnum2 = num2 + txtFries.Text
        calnum3 = num3 + txtDrinks.Text
 
        sum = calnum1 + calnum2 + calnum3
 
        'list of products view
        lstResult1.Items.Clear()
        lstResult1.Items.Add(String.Format(fmtstr, "Item: ", "Quantity", "Price"))
        lstResult1.Items.Add(String.Format(fmtstr, "Pizza Slices", txtPizza.Text, num1))
        lstResult1.Items.Add(String.Format(fmtstr, "Fries", txtFries.Text, num2))
        lstResult1.Items.Add(String.Format(fmtstr, "Soft Drinks", txtDrinks.Text, num3))
        lstResult1.Items.Add(String.Format(fmtstr, "", "", ""))
        lstResult1.Items.Add(String.Format(fmtstr, "Total :", "", sum))
 
    End Sub
 
End Class

Hope this helps.

Edited 6 Years Ago by kinwang2009: n/a

Comments
We do not give homework solutions away in these forums. We help them find the solutions themselves.

Think a little bit about what it is you are doing. You start off with declaring variables for food items with type Double. Then you convert the textbox values to type double and assign to the varialbes. Next you call you displayBill sub passing these three variables as parameters.

Now in displayBill, you are taking arguments num1, num2, and num3. At this point you should forget about using the control properties anywhere in here. But you do this...

'calculation for number of item ordered
calnum1 = txtPizza.Text + num1

Why would you need to calculate the number of items ordered? The textbox should have that, right? And the sub just got that value as num1. Therefore, "txtPizza.Text" and "num1" should be the same. You are doubling the value for number of items. And, because one is a Text datatype, you are adding string text and a number. The result may likely be concatenation of the two as strings. Ex. 1 + 2 = 12.

You then assign the price of each item to the "num" variables.

'Price of each food item
num1 = 1.75

Why are you reusing the variable that holds the number of items purchased? It would be better to define these as constants in the beginning. Also, better to make it clear what it represents: priceFries for example.

In your list of products view, you use "Pizza Slices", txtPizza.Text, num1. Again, using the text property when you passed it into this sub as num1, but then you reassigned num1 to be price. Why did you pass it in?

This same problem was posted in another thread previously.

http://www.daniweb.com/forums/thread207039.html

My guess is that this is a class project. Please look it over and discuss with instructor if this is the case.

Comments
So my explanation wasn't detailed enough?
Public Class Form1

    Const CONST_PIZZA As Double = 1.75, CONST_FRIES As Double = 2.0, CONST_DRINKS As Double = 1.25

    Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        Dim pizza As Double
        Dim fries As Double
        Dim drinks As Double
        Dim sum As Double
        Getnumbers(pizza, fries, drinks)
        CalculateSum(pizza, fries, drinks, sum)
        DisplaySum(pizza, fries, drinks, sum)
    End Sub

    Sub Getnumbers(ByRef pizzaNumber As Double, ByRef friesNumber As Double, ByRef drinksNumber As Double)
        pizzaNumber = CDbl(txtpizza.Text)
        friesNumber = CDbl(txtfries.Text)
        drinksNumber = CDbl(txtdrinks.Text)
    End Sub
    Sub CalculateSum(ByVal pizzaNumber As Double, ByVal friesNumber As Double, ByVal drinksNumber As Double, ByVal sumNumber As Double)
        sumNumber = pizzaNumber + friesNumber + drinksNumber
    End Sub

    Sub DisplaySum(ByVal pizzaNumber As Double, ByVal friesNumber As Double, ByVal drinksNumber As Double, ByVal sumNumber As Double)
        Dim fmtstr As String = "{0, -15} {1, 25} {2,30:c2}"

        'Display the total of three products
        lstresult.Items.Clear()
        lstresult.Items.Add(String.Format(fmtstr, "Item: ", "Quantity", "Price"))
        lstresult.Items.Add(String.Format(fmtstr, "Pizza Slices", txtpizza.Text, pizzaNumber))
        lstresult.Items.Add(String.Format(fmtstr, "Fries", txtfries.Text, friesNumber))
        lstresult.Items.Add(String.Format(fmtstr, "Soft Drinks", txtdrinks.Text, drinksNumber))
        lstresult.Items.Add(String.Format(fmtstr, "", "", ""))
        lstresult.Items.Add(String.Format(fmtstr, "Total :", "", sumNumber))
    End Sub
End Class

After a lot of thinking, that's what i got....
I am stil having issue with the Getnumbers Sub
jeffreyk16 , i am doing my best . if you do not help just don't write anything

Thank you

GetNumbers code is using ByRef, so you should probably use the same variable name as you declared. Seems right, so you will get the value set in the public/global variable by that name.

Dim pizzaNumber As Double
Dim friesNumber As Double
Dim drinksNumber As Double
Dim sumTotal As Double

Sub GetNumbers ...
pizzaNumber = CDbl(txtpizza.Text)
friesNumber = CDbl(txtfries.Text)
drinksNumber = CDbl(txtdrinks.Text)

then the call to Calc sub

CalculateSum(pizzaNumber, friesNumber, drinksNumber, sum)

The CalculateSum sub needs to calculate subtotals for each item, then add the subtotals to get the sum total. Use the formula --
cost = quantity * price
(The fact that you didn't have this formula in the Calc sub is what made me think you should talk with your teacher. They should have provided this to you.)

Sub CalculateSum (... )
'Variable to hold subtotal
Dim subPizza As Double
'Declare the other two
' Then the calc for pizza subtotal would be --
subPizza = pizzaNumber * CONST_PIZZA
'Do the same calc for the other two
'Sum the three subtotals for the sum total
sumTotal = subPizza + subFries + subDrinks

Sorry, I have to go. Did what I could. I think you can get it from there.

This article has been dead for over six months. Start a new discussion instead.