# programming

posted by on .

write a program to calculate the amount of a server's tip given the amount of the bill and the percentage tip ovtained via input dialog boxes. The output should be a complete sentence that reiterates the inputs and gives the resulting tip.

here is my attempt:

Public Class frmTip

Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
Dim billAmount As Double
Dim percent, tip As Double
billAmount = CDbl(InputBox("Amount of Bill"))
percent = CDbl(InputBox("Amount of Tip"))
tip = billAmount * percent / 100
txtOutput.Text = CDbl(Math.Round(tip, 2))
txtOutput.Text = "A percent tip on bill is tip"
End Sub
End Class

• programming - ,

It's been a long time since I looked at programming, but let me guess. All you are getting is something that says, "A percent tip on bill is tip."

Notice what you put in quotation marks.

Again....I could be way off on this. It's been YEARS!

• programming - ,

No you are right. But I'm having trouble getting the correct coding for that step. Any ideas?

• programming - ,

It's from this line:
txtOutput.Text = "A percent tip on bill is tip"

It should say:
txtOutput.Text = "A percent tip on bill is " + tip

I don't remember if you need a + or & or what exactly it is. I think it's a +.

Try that. Again....sorry if I am wrong. It's been a long time.

What is inside quotations will be quoted directly. Things outside the quotation marks are variables. That will cause whatever "tip" is to be printed.

I don't know if your math is right. First see if you can get the program right, then you can fix the rest.

• programming - ,

Matt is right, you can use both + and & to concatenate strings, if they are both strings.
With the +, there is a danger that arithmetic operations will be performed before output, while the & will coerce any numeric variables to strings before output, so & is generally safer.

You can do something like:
txtOutput.text = CDbl(percent) & " percent tip on a bill of " & CDbl(bill) & " is " & CDbl(tip) & " dollars".

You will find a whole lot of decimal figures after the decimal point. You will need to do some formatting, depending on what you have done in your course. One way is to use
String.Format("{0:c}", bill)
to replace bill. "c" is the currency option.
You can also use a single String.Format() statement to output all three variables (percent, bill and tip) together with the embedded text.

I do not have VB.net to test it out, but tell us if you have difficulties.

• programming - ,

Well I used & instead of + which is what we have learned in class. I'm not sure about the string operation.
The program works. The problem I am having is having the currency appear properly. The output should appear as follows: A 15% tip on \$20.00 is \$3.00, but my output appears as: A 15% tip on \$20 is \$3 Here's the program I have so far:

Public Class frmTip

Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
Dim billAmount, tip As Double
Dim percent As Double
billAmount = CStr(InputBox("Amount of Bill"))
percent = CDbl(InputBox("Amount of Tip"))
tip = billAmount * percent / 100
txtOutput.Text = "A " & percent & "% tip on \$" & billAmount & " is \$" & tip
End Sub
End Class

Thank you so much. You are very helpful.

• programming - ,

For the output, there may be a better way to do it, but the following works:

txtOutput.Text = "A " & String.Format("{0:f0}", percent) & "% tip on " & String.Format("{0:c}", billAmount) & " is " & String.Format("{0:c}", tip)

The "{0:f0}" tells the string.Format method to format the next parameter (percent) as a floating number with 0 digits after the decimal point.
The "{0:c}" tells the string.Format method to format the next parameter (bill) as a monetary amount. The \$ will be added if your locale uses dollars. Also, it will add the thousands separators, and 2 digits after the decimal point if this is what your locale defines.

Try it and let us know if you need other information.

• programming - ,

Thank you so much for your help. It finally works. This is what I have:

Public Class frmTip

Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
Dim billAmount, tip As Double
Dim percent As Double
billAmount = CStr(InputBox("Amount of Bill"))
percent = CDbl(InputBox("Amount of Tip"))
tip = billAmount * percent / 100
txtOutput.Text = "A " & percent & "% tip on " & FormatCurrency(billAmount) & " is " & FormatCurrency(tip)
End Sub
End Class

I'll try your solution as well.

• programming - ,

Good job!

If you want perfection, you can do some data checking, such as non-negative bill and percentage etc. and re-prompt for the correct values.

• programming - ,

Good idea I"ll do that. Thank you so much.

• programming :) - ,

It's been a pleasure!

• computer support specialist - ,

Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterData.Click
Dim billAmount, percent As String
Dim tip As Double
billAmount = CStr(InputBox("Amount of Bill"))
percent = CStr(InputBox("Amount of Tip"))
tip = billAmount * percent / 100
txtBoxShow.Text = ("A " & percent & " % tip on " & FormatCurrency(billAmount) & " is " & FormatCurrency(tip))
End Sub