Results 1 to 21 of 21

Thread: [RESOLVED] Calculations help

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2015
    Posts
    17

    Resolved [RESOLVED] Calculations help

    Please help me with my code. Please don't completely redo my code or I'll never learn where i'm going wrong. I appreciate the help.
    Everything to this point is working correctly but when I go to do the calculations they aren't calculating correctly.
    Total rooms, Occupied, vacant, and Occupancy Rate. All incorrect.
    Code:
    Public Class frmGalaxyHotel
    
        Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    
            Dim strOccupied As String
            Dim decOccupied As Decimal
            Dim strVacant As String
            Dim decVacant As Decimal
            Dim decTotalofAllRooms As Decimal
    
            Dim strInputBoxMessage As String = "How many rooms are occupied on floor #"
            Dim strInputBoxHeading As String = "Occupancy"
            Dim strNormalBoxMessage As String = "How many rooms are occupied on floor #"
            Dim strNegativeNumberErrorMessage As String = "Error - Enter a POSITIVE number"
            Dim strNonNumericErrorMessage As String = "Error - Enter NUMBERS ONLY"
            Dim strVInputBoxHeading As String = "Vacancy"
            Dim strVNormalBoxMessage As String = "How many rooms are vacant on floor #"
            Dim strVInputBoxMessage As String = "How many rooms are vacant on floor #"
    
    
            Dim strCancelButtonClicked As String = ""
            Dim intMaximumNumberOfEntries As Integer = 7
            Dim intNumberOfEntries As Integer = 0
    
            strOccupied = "null"
            strVacant = "null"
    
            While intNumberOfEntries < intMaximumNumberOfEntries _
            And Not strOccupied = strCancelButtonClicked _
            And Not strVacant = strCancelButtonClicked
    
                intNumberOfEntries += 1
    
                Do
    
                    strOccupied = InputBox(strInputBoxMessage & intNumberOfEntries, strInputBoxHeading, " ")
                    strInputBoxMessage = strNormalBoxMessage
    
                    If IsNumeric(strOccupied) Then
                        decOccupied = Convert.ToDecimal(strOccupied)
                        If decOccupied > 0 Then
                            decTotalofAllRooms += decOccupied
                        Else
                            strInputBoxMessage = strNegativeNumberErrorMessage
                        End If
                    Else
                        strInputBoxMessage = strNonNumericErrorMessage
                    End If
    
                Loop Until strInputBoxMessage = strNormalBoxMessage Or strOccupied = strCancelButtonClicked
    
                If strOccupied = strCancelButtonClicked Then
                    Exit While
                End If
    
                Do
    
                    strVacant = InputBox(strVInputBoxMessage & intNumberOfEntries, strVInputBoxHeading, " ")
                    strVInputBoxMessage = strVNormalBoxMessage
    
                    If IsNumeric(strVacant) Then
                        decVacant = Convert.ToDecimal(strVacant)
                        If decVacant > 0 Then
                            decTotalofAllRooms += decVacant
                        Else
                            strVInputBoxMessage = strNegativeNumberErrorMessage
                        End If
                    Else
                        strVInputBoxMessage = strNonNumericErrorMessage
                    End If
    
                Loop Until strVInputBoxMessage = strVNormalBoxMessage _
                Or strVacant = strCancelButtonClicked
    
                If Not strVacant = strCancelButtonClicked Then
                    lstHotel.Items.Add("Floor " & intNumberOfEntries & " Occupied " & decOccupied & " Vacant " & decVacant)
                End If
    
            End While
    
            lblTotalRooms.Text = "Total Rooms: " & (decOccupied + decVacant).ToString
            lblTotalRooms.Visible = True
            lblOccupied.Text = "Total Rooms Occupied: " & decOccupied.ToString
            lblOccupied.Visible = True
            lblVacant.Text = "Total Rooms Vacant: " & decVacant.ToString
            lblVacant.Visible = True
            lblRate.Text = "Occupancy Rate: " & (100 / (decOccupied + decVacant) * strOccupied).ToString("n0") & "%"
            lblRate.Visible = True

  2. #2
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Calculations help

    Ignoring the code, what are the calculations supposed to be?, can you write them out?


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2015
    Posts
    17

    Re: Calculations help

    It is supposed to calculate total rooms that a person inputs for 7 floors. Displaying the Total rooms, occupied rooms, vacant rooms, and occupancy rate (all from user input).
    I'm attaching a screen shot of my design. You'll see where it's showing the calculations wrong.
    Can you tell me where i'm ignoring the code? I've written this thing over and over trying to figure it out. I get one thing to work and then another problem shows up.
    Name:  GalaxyHotel.jpg
Views: 65
Size:  41.2 KB

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,393

    Re: Calculations help

    This is the algorithm in pseudocode:

    Loop
    Input value for occupied
    Add to occupied total
    Input value for vacant
    Add to vacant total
    Calculate occupancy rate = (100 / (occupied + vacant)) * occupied
    Output values to textboxes
    Keep looping specified number of times
    Last edited by .paul.; Mar 23rd, 2015 at 07:04 PM.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2015
    Posts
    17

    Re: Calculations help

    So here is what I have right now..I commented out the others so I could work one at a time.
    with the code below I can get total of rooms to now say 27 (which is right)
    however, the occupancy rate says 33.33333333%???

    Code:
    lblTotalRooms.Text = strVacant + strOccupied
            lblTotalRooms.Visible = True
            lblTotalRooms.Text = "Total Rooms: " & decTotalofAllRooms.ToString("G")
            lblRate.Text = "Occupancy Rate: " & (100 / (decOccupied + decVacant)) * decOccupied.ToString("n0") & "%"
            lblRate.Visible = True
            lblVacant.Visible = True
            lblOccupied.Visible = True

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,393

    Re: Calculations help

    It's a good idea to try to isolate the problem in a case like yours.
    The occupancy formula is:

    (100 / total rooms) * total occupied rooms

    If you want to format to a specified number of places, call .ToString("n0") for no decimal places or .ToString("n1") for 1 decimal place, etc

  7. #7
    Lively Member
    Join Date
    Sep 2011
    Posts
    110

    Re: Calculations help

    Edit: NM my math sucked.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Mar 2015
    Posts
    17

    Re: Calculations help

    Its supposed to be whole percentage so I'll put it as N0
    I still can't get it to calculate the percentage correctly. Now i get 3.7097037037037%
    UGh math
    Code:
    lblRate.Text = "Occupancy Rate: " & (100 / decTotalofAllRooms) * decOccupied.ToString("N0") & "%"

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,393

    Re: Calculations help

    Code:
    Dim intOccupiedTotal As Integer = 18
    Dim intVacantTotal As Integer = 10
    Dim intTotalofAllRooms As Integer = intOccupiedTotal + intVacantTotal
    MsgBox(((100 / intTotalofAllRooms) * intOccupiedTotal).ToString("n0"))

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Mar 2015
    Posts
    17

    Re: Calculations help

    I can't have set numbers. The available and occupied are up to the user(instructor) to input.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Mar 2015
    Posts
    17

    Re: Calculations help

    Code:
    lblRate.Text = "Occupancy Rate: " & (((100 / decTotalofAllRooms) * decOccupied).ToString("N0"))
    doesn't work either.

  12. #12
    Lively Member
    Join Date
    Sep 2011
    Posts
    110

    Re: Calculations help

    Uhmmm... your code only tracks the total number of rooms (which you later never use) in decTotalofAllRooms, but you never track the total of all OCCUPIED rooms. So all of your information, at least in the code provided above, is only about the last floor, not the whole thing. Using the DecTotalofAllRooms would get you what percentage of occupied rooms on the last floor (only) is out of all rooms in the hotel.

    Solution: Make another variable to convert numbers and add it to Vacant and Total as you go.

    Add the following:
    Dim convertToNumber as Decimal

    Then change the following:

    Code:
                        decOccupied = Convert.ToDecimal(strOccupied)
                        If decOccupied > 0 Then
                            decTotalofAllRooms += decOccupied
    to

    Code:
                        convertToNumber = Convert.ToDecimal(strOccupied)
                        If convertToNumber > 0 Then
                            decTotalofAllRooms += convertToNumber
                            decOccupied += convertToNumber
    And do the same for Vacant below it.
    Last edited by OddGamer; Mar 23rd, 2015 at 07:57 PM.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Mar 2015
    Posts
    17

    Re: Calculations help

    I was actually just thinking about that. The Occupancy rate i was getting was only for one floor.
    So...what am I missing to get it to calculate all floors?
    Where am I missing code for that?
    Suggestions?

  14. #14
    Lively Member
    Join Date
    Sep 2011
    Posts
    110

    Re: Calculations help

    Edited my above to show.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Mar 2015
    Posts
    17

    Re: Calculations help

    convertToNumber...as string? Sorry I'm a newbie.
    Trying to compare what I have to what you put there.

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Mar 2015
    Posts
    17

    Re: Calculations help

    I made the change and tried a few things. It did calculate but then it messed up my list box and started adding in there. I can't have it adding it in there. Can only add down at the label stage. Listbox has to only list what the user inputs like my picture above.

  17. #17
    Lively Member
    Join Date
    Sep 2011
    Posts
    110

    Re: Calculations help

    Yeah, I messed it up. Take out my convertToNumber. Here:
    Two variables added. Only lines I changed are shown. Tested and working as far as I can see.
    Code:
            Dim decTotalOccupied As Decimal
            Dim decTotalVacant As Decimal
    
                        decOccupied = Convert.ToDecimal(strOccupied)
                        If decOccupied > 0 Then
                            decTotalofAllRooms += decOccupied
                            decTotalOccupied += decOccupied
    
                        decVacant = Convert.ToDecimal(strVacant)
                        If decVacant > 0 Then
                            decTotalofAllRooms += decVacant
                            decTotalVacant += decVacant
    
            lblTotalRooms.Text = "Total Rooms: " & (decTotalofAllRooms).ToString
            lblOccupied.Text = "Total Rooms Occupied: " & decTotalOccupied.ToString
            lblVacant.Text = "Total Rooms Vacant: " & decTotalVacant.ToString
            lblRate.Text = "Occupancy Rate: " & (100 / (decTotalofAllRooms) * decTotalOccupied).ToString("n0") & "%"

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Mar 2015
    Posts
    17

    Re: Calculations help

    Thank you thank you thank you. That is what did it. Now I think everything is showing correctly at the bottom. You are the best.
    I've been struggling now I know what I needed to add to make it work correctly. AHHH relief.

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Mar 2015
    Posts
    17

    Re: Calculations help

    How do I change this to Resolved up at the top?

  20. #20
    Lively Member
    Join Date
    Sep 2011
    Posts
    110

    Re: Calculations help

    Just above the first post in this thread you'll see a drop down menu on the right side called "Thread Tools". Mark as resolved is in there.
    And you're welcome.

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Mar 2015
    Posts
    17

    Re: [RESOLVED] Calculations help

    Thank you ALL again for your help. I've been struggling for over a wee with this program. Now i see what I was missing and now i know for the future. YAY!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width