i\'m working on a visual basic problem for homework. I\'m supposed to be making
ID: 3621066 • Letter: I
Question
i'm working on a visual basic problem for homework. I'm supposed to be making a form to display the total cost of a restaurant bill. It's asking for a sub procedure for the quantity of item inputs, then a function to calculate total cost, and another sub procedure to display everything.This is what i have so far:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fmtstr As String = "{0,-10}{1,20}{2,50}"
Dim pizza, fries, drinks, p1, p2, p3, total As Double
p1 = 1.75
p2 = 2.0
p3 = 1.25
input(pizza, fries, drinks)
disp(fmtstr, pizza, fries, drinks, p1, p2, p3, total)
Calc(pizza, fries, drinks, total)
End Sub
Sub input(ByRef pizza As Double, ByRef fries As Double, ByRef drinks As Double)
pizza = TextBox1.Text
fries = TextBox2.Text
drinks = TextBox3.Text
End Sub
Sub disp(ByVal fmtstr As String, ByVal pizza As Double, ByVal fries As Double, ByVal drinks As Double, ByVal p1 As Double, ByVal p2 As Double, ByVal p3 As Double, ByVal total As Double)
ListBox1.Items.Clear()
ListBox1.Items.Add(String.Format(fmtstr, "Item", "Quantity", "Price"))
ListBox1.Items.Add("")
ListBox1.Items.Add(String.Format(fmtstr, "Pizza Slices", pizza, (FormatCurrency(p1))))
ListBox1.Items.Add(String.Format(fmtstr, "Fries", fries, (FormatCurrency(p2))))
ListBox1.Items.Add(String.Format(fmtstr, "Soft Drinks", drinks, (FormatCurrency(p3))))
fmtstr = "{0,-10}{1,70}
ListBox1.Items.Add(String.Format(fmtstr, "Total", (FormatCurrency(total))))
End Sub
Function Calc(ByRef pizza As Double, ByRef fries As Double, ByRef drinks As Double, ByVal total As Double) As Double
total = pizza + fries + drinks
Return total
End Function
End Class
The only problem i'm having is that the total comes up as $0.00
What am i doing wrong? textbox1,2,3 are the input boxes where the number of items is put.
im guessing that the problem is that im programming it wrong where the values of the items input aren't reading. p1,2,3 are the prices for the items.
Any help would be greatly appreciated.
Explanation / Answer
Hi,
1) Modify your code for button click
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fmtstr As String = "{0,-10}{1,20}{2,50}"
Dim pizza, fries, drinks, p1, p2, p3, total As Double
p1 = 1.75
p2 = 2.0
p3 = 1.25
input(pizza, fries, drinks)
total = Calc(pizza, fries, drinks, p1, p2, p3, total)
disp(fmtstr, pizza, fries, drinks, p1, p2, p3, total)
End Sub
2) And Function Calc is not calculating the prices because pizza, fries, drinks are the item Quantities. You can modify Calc function as below
Function Calc(ByRef pizza As Double, ByRef fries As Double, ByRef drinks As Double, ByRef p1 As Double, ByRef p2 As Double, ByRef p3 As Double, ByVal total As Double) As Double
total = (pizza * p1) + (fries * p2) + (drinks * p3)
Return total
End Function
I hope it helps :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.