0

Basically for some reason when I run this problem it doesn't display the room type (which should be Studio, Moderate, Luxe, Deluxe, or Suite) and it doesn't display my daily rate or total rate. I've been sitting on this assignment for quite a few hours and I can't figure out what seems to be the issue. I think it might have something to do with my ElseIf statement or Global Constants but I'm not sure what I did wrong.

rbb
  • 1
  • 1
  • You have not provided any useful information for us to help us debug it. What inputs are you using ? What is the result you are getting ? What is your desired result ? – Shai Rado Apr 04 '17 at 17:05
  • I'm sure you can do a lot of this yourself - step through the code, add a breakpoint etc? – SJR Apr 04 '17 at 17:08
  • One thing, your formula (most likely) of `Total = RmCost * NumDays * Tip` , should be `Total = RmCost * NumDays * (1 + Tip/100)` – Shai Rado Apr 04 '17 at 17:08
  • For example, if I enter 2 for the first InputBox (room type), and put 3 for the 2nd InputBox (days) and put 10 for the 3rd InputBox (tip) then I get the following MsgBox: "Room Type: . Daily Rate: 0. Number of days: 3. Total Charge: 0 Note that the charge includes the 10% tip." – rbb Apr 04 '17 at 17:12
  • what are you trying to do with such a statement as `RmCost = Studio And RmName = "Studio"`? – user3598756 Apr 04 '17 at 17:14
  • There is no breakpoint, it compiles and runs. It just produces wrong output. – rbb Apr 04 '17 at 17:14
  • Your If clauses are wrong - you need to split `RmCost = Studio And RmName = "Studio"` into two lines and lose the `And`. – SJR Apr 04 '17 at 17:14
  • RmCost = Studio is storing the global constant of Studio (at the very top) in RmCost so I can output the number that way (per instructions of professor, I know inputting the values manually would be easier. RmName = "Studio" is storing the name Studio in the RmName variable. – rbb Apr 04 '17 at 17:15
  • You are right it works now! When I changed it from RmCost = Studio and RmName = "Studio" To RmCost = Studio RmName = "Studio" Why did it not work with And? – rbb Apr 04 '17 at 17:17
  • See the answer below for explanation. Perhaps you'd care to acknowledge that it helped you? – SJR Apr 04 '17 at 17:22
  • How do I do that? – rbb Apr 04 '17 at 17:26

2 Answers2

2

You're trying to set values to variables using the And operator. This isn't correct, because the And operator is a logical operator.

Performs a logical conjunction on two Boolean expressions, or a bitwise conjunction on two numeric expressions.


I.e.:

When you write:

If RmType = 1 Then
    RmCost = Studio And RmName = "Studio"

The output of this line RmCost = Studio And RmName = "Studio" will be True or False.

You're not setting any value to RmCost and RmName!


So, instead of :

If RmType = 1 Then
        RmCost = Studio And RmName = "Studio"
ElseIf RmType = 2 Then
        RmCost = Moderate And RmName = "Moderate"

You should use:

If RmType = 1 Then
    RmCost = Studio
    RmName = "Studio"
ElseIf RmType = 2 Then
    RmCost = Moderate
    RmName = "Moderate"

And instead of:

If RmType = 1 And NumDays > 2 Then
    RmType = 2 And MsgBox("bla bla bla")

Use:

If RmType = 1 And NumDays > 2 Then
    RmType = 2
    MsgBox("bla bla bla")
dot.Py
  • 5,007
  • 5
  • 31
  • 52
0

The reasons for crucial part of your error were already answered in the comments and the answer by @dot.Py (by the time I've finished typing this code).

I've just added a few other corrections (missing variable declarations, replaced Integer with Long - read HERE , and on MSDN), and replacing your multiple Ifs with Select Case.

Code

Option Explicit

Global Const Studio As Long = 75, Moderate As Long = 100, Luxe As Long = 150, Deluxe As Long = 300, Suite As Long = 500

Sub Charge()

Dim Tip As Double
Dim Total As Long
Dim RmName As String
Dim RmCost As Long
Dim RmType As Long
Dim NumDays As Long

RmType = InputBox("What room type would you like ? Enter '1' for Studio, '2' for Moderate, '3' for Luxe, '4' for Deluxe, and '5' for Suite.")

Select Case RmType
    Case 1
        RmCost = Studio
        RmName = "Studio"
    Case 2
        RmCost = Moderate
        RmName = "Moderate"
    Case 3
        RmCost = Luxe
        RmName = "Luxe"
    Case 4
        RmCost = Deluxe
        RmName = "Deluxe"
    Case 5
        RmCost = Suite
        RmName = "Suite"
    Case Is < 1, Is > 5
        MsgBox "Incorrect Data Entry"
        Exit Sub
End Select

NumDays = InputBox("How many days would you like to stay ?")    
Tip = InputBox("Enter in desired gratuity. For x% enter x.")
Tip = Tip / 100    
Total = RmCost * NumDays * Tip

If RmType = 1 And NumDays > 2 Then
    RmType = 2
    MsgBox "Studio type isn't available for more than 2 nights. You're upgraded to a Moderate option." & vbNewLine & "Room Type: " & _
        RmName & ". Daily rate: " & RmCost & ". Number of days: " & NumDays & "." & _
        "Total Charge: " & Total & vbCrLf & vbCrLf & "Note that the charge includes the " & _
        Tip * 100 & "% tip."
Else
    MsgBox "Room Type: " & RmName & ". Daily rate: " & RmCost & ". Number of days: " & _
        NumDays & ". Total Charge: " & Total & vbCrLf & vbCrLf & _
        "Note that the charge includes the " & Tip * 100 & "% tip."
End If

End Sub
Community
  • 1
  • 1
Shai Rado
  • 33,032
  • 6
  • 29
  • 51