Friday
March 24, 2017

Post a New Question

Posted by on Friday, October 22, 2010 at 8:41am.

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 - , Friday, October 22, 2010 at 9:17am

    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 - , Friday, October 22, 2010 at 9:40am

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

  • programming - , Friday, October 22, 2010 at 10:28am

    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 - , Friday, October 22, 2010 at 11:34am

    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 - , Friday, October 22, 2010 at 5:22pm

    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 - , Friday, October 22, 2010 at 9:47pm

    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 - , Friday, October 22, 2010 at 11:33pm

    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 - , Friday, October 22, 2010 at 11:50pm

    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 - , Saturday, October 23, 2010 at 2:44am

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

  • programming :) - , Saturday, October 23, 2010 at 4:14pm

    It's been a pleasure!

  • computer support specialist - , Wednesday, February 20, 2013 at 2:57pm

    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

Answer This Question

First Name:
School Subject:
Answer:

Related Questions

More Related Questions

Post a New Question