dcsimg
Results 1 to 18 of 18

Thread: New Programmer, need help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    New Programmer, need help

    I am new to programming and am taking a vb.net class. I need help on an assignment. Below is the code I have written so far. I have to labels that are supposed to output the total rooms occupied in the hotel, and the total occupancy rate. Also, an input box comes up when btnReport is clicked. The user inputs the number of rooms occupied on that floor and then in the list box it displays the number of rooms occupied on that floor and the occupancy rate for that particular floor. The program has no problem getting the rooms occupied for each floor and no problem accumulating the total rooms occupied, but for all of my percentages I keep getting 0. I need to know what I am doing wrong that is causing only 0 percentages.

    Here is my code:

    Option Strict On
    Option Explicit On
    Public Class frmHotelOccupancy
    Private Sub btnReport_Click(sender As Object, e As EventArgs) Handles btnReport.Click
    Dim shoCount As Short = 1
    Dim strOccupiedRooms As String = CStr(0)
    Const ROOMS_ON_FLOOR As Short = 30
    Const PERCENT As Short = 100
    Dim decOccupancyRate As Decimal = CDec((CShort(strOccupiedRooms) / ROOMS_ON_FLOOR) * PERCENT)
    Dim shoTotalRoomsOcc As Short = 0
    Dim decTotalOccRate As Decimal

    decTotalOccRate = CDec((shoTotalRoomsOcc / 240) * PERCENT)

    For shoCount = 1 To 8
    strOccupiedRooms = InputBox("Please Enter the Number of Occupied Rooms for floor " & shoCount & ".", "Hotel Occupancy")
    lstRoomInput.Items.Add("Floor:" & shoCount.ToString & " Rooms Occupied:" & strOccupiedRooms & " Occupancy Rate:" &
    decOccupancyRate.ToString & "%")
    shoTotalRoomsOcc += CShort(strOccupiedRooms)


    Next
    lblRoomsOccupied.Text = shoTotalRoomsOcc.ToString
    lblOccupancyRate.Text = decTotalOccRate & "%"

    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Me.Close()
    End Sub

    Private Sub frmHotelOccupancy_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    If MessageBox.Show("Are you sure?", "Quit Program?", MessageBoxButtons.YesNo, _
    MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = _
    Windows.Forms.DialogResult.No Then
    e.Cancel = True
    End If
    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
    lstRoomInput.Items.Clear()
    lblOccupancyRate.Text = ""
    lblRoomsOccupied.Text = ""
    btnReport.Focus()
    End Sub
    End Class

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,527

    Re: New Programmer, need help

    Welcome to the forums.

    I think your problem's here:-
    Code:
    decTotalOccRate = CDec((shoTotalRoomsOcc / 240) * PERCENT)
    Both shoTotalRoomsOcc and 240 are integer types so you're doing an integer division. In other words that's going to get rounded to the closest integer value. That will be either 0 or 1 (probably 0). Then you multiply it by percent which is also an integer type and only then do you convert it to a decimal. Try wrapping the shoTotalRoomsOcc in a CDec before you do the division and the problem should go away.
    You can depend upon the Americans to do the right thing. But only after they have exhausted every other possibility - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    Re: New Programmer, need help

    That did not fix it but I think it may have been part of the problem. I needed to move the Dec Occupancy Rate variable to be just underneath the strOccupiedRooms in the For statement.
    My main problem now is that I can't get it to Round to the 2nd decimal place.

    Also, I need it to have exception handlers to make the program give an error message to the user if the user enters a letter or a negative number, or a number great then 30. How would I go about doing that?

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,914

    Re: New Programmer, need help

    Integer / Integer results in a Double. Integer \ Integer is what results in an Integer, so I don't agree with what FD suggested. The reason you keep getting 0 is because you never make it anything other than 0.

    There are actually a couple things wrong with the code. First off, you make the OccupiedRooms a string, when it is clearly intended to hold a numeric value. Technically, you are doing conversions from a string back to a number, so you are converting it, but why is it a string in the first place? All that conversion is largely a waste of time. You need the variable to be a number, make it a number and leave it that way.

    Second, you do this to calculate the occupancy rate:

    Dim decOccupancyRate As Decimal = CDec((CShort(strOccupiedRooms) / ROOMS_ON_FLOOR) * PERCENT)

    That's fine, but strOccupiedRooms is 0, so the occupancy rate is 0, as well. You then go into a loop asking the user for a new occupancy. Were you expecting that changing strOccupiedRooms would automatically recalculate the decOccupancyRate? That's not what will happen. Running that equation earlier doesn't somehow link strOccupiedRooms to decOccupancyRate. You have to repeat that calculation every time you change strOccupiedRooms. However, since you used the InputBox, you will have other problems, as well. If the user presses Cancel, or enters something that isn't a number, you will get an exception if you try to convert that string into a number using CShort. What you need to do is get the number from the user, then use Integer.TryParse to convert the string into an integer, and use that in the equation.

    To display a Decimal or Double with only two decimal places, show it with .ToString("N2").

    Is it part of the assignment that you have to use an exception handler for that error message, because that is not the right place to use exception handling. Exception handling is slow if an exception is thrown, so you only want to use it for truly exceptional circumstances, and the user entering bad data is far from exceptional. What you should be doing is using Integer.TryParse to see whether or not the user entered a valid integer, then checking that integer to see if it is less than 0 to rule out negative numbers. None of that requires exception handling, though.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    Re: New Programmer, need help

    Quote Originally Posted by Shaggy Hiker View Post
    Integer / Integer results in a Double. Integer \ Integer is what results in an Integer, so I don't agree with what FD suggested. The reason you keep getting 0 is because you never make it anything other than 0.

    There are actually a couple things wrong with the code. First off, you make the OccupiedRooms a string, when it is clearly intended to hold a numeric value. Technically, you are doing conversions from a string back to a number, so you are converting it, but why is it a string in the first place? All that conversion is largely a waste of time. You need the variable to be a number, make it a number and leave it that way.

    Second, you do this to calculate the occupancy rate:

    Dim decOccupancyRate As Decimal = CDec((CShort(strOccupiedRooms) / ROOMS_ON_FLOOR) * PERCENT)

    That's fine, but strOccupiedRooms is 0, so the occupancy rate is 0, as well. You then go into a loop asking the user for a new occupancy. Were you expecting that changing strOccupiedRooms would automatically recalculate the decOccupancyRate? That's not what will happen. Running that equation earlier doesn't somehow link strOccupiedRooms to decOccupancyRate. You have to repeat that calculation every time you change strOccupiedRooms. However, since you used the InputBox, you will have other problems, as well. If the user presses Cancel, or enters something that isn't a number, you will get an exception if you try to convert that string into a number using CShort. What you need to do is get the number from the user, then use Integer.TryParse to convert the string into an integer, and use that in the equation.

    To display a Decimal or Double with only two decimal places, show it with .ToString("N2").

    Is it part of the assignment that you have to use an exception handler for that error message, because that is not the right place to use exception handling. Exception handling is slow if an exception is thrown, so you only want to use it for truly exceptional circumstances, and the user entering bad data is far from exceptional. What you should be doing is using Integer.TryParse to see whether or not the user entered a valid integer, then checking that integer to see if it is less than 0 to rule out negative numbers. None of that requires exception handling, though.
    I just need it to not crash when an invalid number or letter is input. I will try to rewrite it using the methods you mentioned and see if it works correctly. Thanks for the help!

  6. #6
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: New Programmer, need help

    One last thing that I noticed... You are using the Short DataType, which while is ok to use, it is not the best choice performance-wise. Integers perform the fastest of any numeric type in VB but at the cost of size when compared with Shorts. Short (and Byte for that matter) are better than Integers when space is limited, but since that's not going to be the case for this program, then I'd suggest using Integers instead. That being said, both the performance issues as well as the space considerations are all but moot for this program (and probably for any assignment), so I wouldn't bother changing this program just for changing the DataTypes; I'm mentioning this more as an FYI for future consideration.

    Also I forgot to mention... Although Shaggy's comment about not using strOccupiedRooms as a variable for numeric values is correct, I'd still keep that particular variable (as a String DataType), but in addition I'd have an intOccupiedRooms as well. The reason is that you will probably want a String variable to hold whatever the user typed into the InputBox, but you'll want to then convert that String to a number using TryParse. So your TryParse line would look like:
    Code:
    If Not Integer.TryParse(strOccupiedRooms, intOccupiedRooms) OrElse intOccupiedRooms < 0 OrElse intOccupiedRooms > ROOMS_ON_FLOOR Then
        ' Alert user to incorrect input and allow user to re-enter in a number
    End If
    Last edited by Pyth007; Mar 6th, 2015 at 02:33 PM. Reason: Keep strOccupiedRooms...

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    Re: New Programmer, need help

    Quote Originally Posted by Pyth007 View Post
    One last thing that I noticed... You are using the Short DataType, which while is ok to use, it is not the best choice performance-wise. Integers perform the fastest of any numeric type in VB but at the cost of size when compared with Shorts. Short (and Byte for that matter) are better than Integers when space is limited, but since that's not going to be the case for this program, then I'd suggest using Integers instead. That being said, both the performance issues as well as the space considerations are all but moot for this program (and probably for any assignment), so I wouldn't bother changing this program just for changing the DataTypes; I'm mentioning this more as an FYI for future consideration.

    Also I forgot to mention... Although Shaggy's comment about not using strOccupiedRooms as a variable for numeric values is correct, I'd still keep that particular variable (as a String DataType), but in addition I'd have an intOccupiedRooms as well. The reason is that you will probably want a String variable to hold whatever the user typed into the InputBox, but you'll want to then convert that String to a number using TryParse. So your TryParse line would look like:
    Code:
    If Not Integer.TryParse(strOccupiedRooms, intOccupiedRooms) OrElse intOccupiedRooms < 0 OrElse intOccupiedRooms > ROOMS_ON_FLOOR Then
        ' Alert user to incorrect input and allow user to re-enter in a number
    End If
    This last bit was very helpful. As for why I am using short, my teacher deducts for using larger then necessary data types.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,914

    Re: New Programmer, need help

    Your teacher is a loon. There is some point to that, but not a whole lot. The 32-bit CPU doesn't move around less than 32 bits at a time, and storage of variables in memory is done in a fashion that optimizes performance, rather than memory savings. There is a minor performance hit for variables larger than an integer, and probably no real performance hit for variables smaller than an integer, but those smaller ones are probably still held in four bytes on a 32-bit CPU (or 8 bytes on a 64-bit CPU, most likely). I don't believe there even exists an instruction to move something as small as one byte in or out of an x86 CPU, though there is a means to deal with two bytes (I just forget what it is).

    So, I have never used a Short, because it may (or may not) cost you anything in performance, but it gains you nothing in saved memory. Having said that, I would note that I do use bytes where appropriate (generally in arrays or Lists). Still, it's kind of nutty to penalize somebody for using an Integer where a Short would suffice, because the Integer is the better choice in all circumstances.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    Re: New Programmer, need help

    This is a little off subject, but I took this class just to see what I thought of it. I'm actually a business major and I'm in too deep to Change my major. But I have major interest in computer science now recently. Where is the best place to learn computer Science things of all sorts for beginner's, any good books, websites, apps?

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,914

    Re: New Programmer, need help

    A bunch of us, myself included, are self-taught. I feel that the best way is just to dive in. The hardest part about that is coming up with a project challenging enough for you to learn and tractable enough that you can solve it.
    My usual boring signature: Nothing

  11. #11
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: New Programmer, need help

    To be honest, the best choice for learning programming is C (or possibly Assembler). If you can stomach the tedium of making bits and bytes do what you want, and understand the intricacies of how the computer works, you'll find out how easy it is to program in some higher level language, and appreciate the heavy lifting it does for you.

    Alas, I have a suspicion that programming courses don't do such things, today.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,914

    Re: New Programmer, need help

    I think that knowing something about Assembler is really useful, but the tedium of some of those languages will drive away lots of people who are starting out. You are unlikely to use Assembler or even C in a job, unless you are doing some fairly specialized stuff. Still, at least the introduction to most assembler books covers a lot about how computers work, and a bit of understanding pointers (which I have generally seen explained better in C or C++ books) is certainly useful in all languages. You may actually make use of C, though it is a fairly special purpose language, these days.
    My usual boring signature: Nothing

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    Re: New Programmer, need help

    I've started Learning C++ on an app I have but I don't get to do a lot of practicing with it. What is Assembler? And what's a good way to learn about the hardware side of computers?

  14. #14
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,335

    Re: New Programmer, need help

    Quote Originally Posted by GetOverHere89 View Post
    What is Assembler?
    Rule number One. Research!!!! Google can return more then we can ever write.Generally it's One question per thread. Please try to stick by this.

    Google assembler = http://en.wikipedia.org/wiki/Assembly_language was not hard?

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Mar 2015
    Posts
    73

    Re: New Programmer, need help

    Sorry, didn't think about that. Will do in the future. Thanks!

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,914

    Re: New Programmer, need help

    Assembler is a good way to learn about the hardware side of computers as far as CPU and memory use is concerned.
    My usual boring signature: Nothing

  17. #17
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: New Programmer, need help

    While for 'general purpose' programming, Assembler and C are not used, if you do know these languages it will do two things: increase you understanding of general programming and especially making efficient use of higher level code and troubleshooting/debugging. Increase your base pay if you do land a job which requires such coding skills.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  18. #18
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: New Programmer, need help

    Quote Originally Posted by Shaggy Hiker View Post
    Your teacher is a loon. There is some point to that, but not a whole lot. The 32-bit CPU doesn't move around less than 32 bits at a time, and storage of variables in memory is done in a fashion that optimizes performance, rather than memory savings. There is a minor performance hit for variables larger than an integer, and probably no real performance hit for variables smaller than an integer, but those smaller ones are probably still held in four bytes on a 32-bit CPU (or 8 bytes on a 64-bit CPU, most likely). I don't believe there even exists an instruction to move something as small as one byte in or out of an x86 CPU, though there is a means to deal with two bytes (I just forget what it is).

    So, I have never used a Short, because it may (or may not) cost you anything in performance, but it gains you nothing in saved memory.
    Actually, it is precisely because Shorts are 16-bit DataTypes that cause their performance to be less than an Integer... When the program does any calculation with a DataType that is less than 32-bits which is the size of the CPU register in x86 systems, that number has to be transformed into the native register size for the CPU to process. Thus that extra overhead will cause DataTypes less than 32-bits to be slightly slower than their optimized counterpart. However the time involved is rather minute, so unless you are doing a lot of loops or other calculations with a Short, that extra bit of processing time won't really matter.

    As for the size, according to MSDN, the .Net framework can optimize the space used to store variables if using multiple smaller sized DataTypes. For example, if you create a Struct of 2 Shorts, it will occupy the same amount of space as an Integer. However once you actually use the variable, it needs to be changed to a 32-bit size in order for the CPU to process it. And since most value types (like Integers and Shorts) are typically used in relatively short order from when they are created, the space consideration is even more minor than the performance hit.

    I do agree that the teacher is an idiot... Although I definitely could see docking points for using an incorrect DataType (or worse or not explicitly declaring DataTypes), I don't see that using an Integer instead of a Short is worth deducting points on; as I sort-of argued, the use of one over the other is largely whichever impact is your greatest concern for the program: speed vs. size. And when we're talking about a few bytes on a system that has a few hundred Gigs, I tend to favor speed over size. However if you really will get penalized for using a DataType that is too large for what is necessary, then I'd recommend changing your Shorts to Bytes as a Byte can hold any positive integer in the range 0 to 255, and none of your variables need any number greater than 255...

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width