PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] Beginner Issues with Calculations-VBForums
Results 1 to 11 of 11

Thread: [RESOLVED] Beginner Issues with Calculations

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    3

    Resolved [RESOLVED] Beginner Issues with Calculations

    Morning All
    I am building a program that calculates the number of books by points.
    Ex: 1-3 books read are worth 10 points, 4-6 are worth 15 and > 6 are worth 20.
    The issue I'm having is getting it to calculate the the first 3 books, the 2nd 3 books and then the > 6.
    Ex: User inputs 8 books read. It should display 115 in the label.


    1. I'm using an If statement (should I be)
    2. Could I use a decimal for the > 6?
    Ex: intTotalNumberOfBooks = (intTotalNumberOfBooks * intFirst3Books) *#D

    I know this is something simple and it's out of VB 101, but it is my very first programming class and my professor uses a copy paste method during class.
    Thank You in advance.

  2. #2
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Re: Beginner Issues with Calculations

    It's not much difficult.
    follow this tutorial http://www.homeandlearn.co.uk/NET/vbNet.html. post your code and ask questions.

  3. #3
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Beginner Issues with Calculations

    I suggest using the site Aash pointed out. I used to to begin my C# excursion. It is very good, excellent for beginners.

    Quote Originally Posted by Soron358 View Post
    1. I'm using an If statement (should I be)
    You will need an if statement whenever you need to decide something. Since you need to decide 3 levels of points, you will need AT LEAST 3 if statements. You can use If ... Else or Select Case, but if your instructor didn't cover those then it is probably best to use just If.


    Quote Originally Posted by Soron358 View Post
    2. Could I use a decimal for the > 6?
    I use a decimal to separate integer components of a numeric value from the fractional part, so I have no idea what you mean by this.


    Quote Originally Posted by Soron358 View Post
    Ex: intTotalNumberOfBooks = (intTotalNumberOfBooks * intFirst3Books) *#D.
    That is one formula. Although it doesn';t make sense to me because you ask to calculate points, so I would expect to see total points to the left of the equal sign, not total number of books unless I misunderstand you. And if I do understand you, then you will either need a very complex formula (which I do not recommend because complex formulas are a poor choice because of maintenance) or 3 formulas since you have 3 different computations.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,266

    Re: Beginner Issues with Calculations

    You will need a for loop from 1 to the number of books read(call the loop variable count)

    in the for loop

    add 10 to the point total

    if the count >= 4 add 5 additional points
    if the count >6 add 5 additional points
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    3

    Re: Beginner Issues with Calculations

    Thank you for the link. I wish my book explained things like this site.


    Quote Originally Posted by MarMan View Post
    You will need an if statement whenever you need to decide something. Since you need to decide 3 levels of points, you will need AT LEAST 3 if statements. You can use If ... Else or Select Case, but if your instructor didn't cover those then it is probably best to use just If.

    I am more comfortable with Select Case then If. I wasn't sure if I could use it for this type of calculation. I am totally green to this and my course book reads horrible.

    Quote Originally Posted by MarMan View Post
    I use a decimal to separate integer components of a numeric value from the fractional part, so I have no idea what you mean by this.
    intFirst3 = 10
    I did this because the first 3 books call for 10 points per book
    intTotalBooksRead += intFirst3 gives me the 10 points per book

    For the next 3 I tried using
    intTotalBooksRead = (intTotalBooksRead * intFirst3) * .5D

    The problem I ran into was with the > 6 books. Cant multiply by 1.0D
    and Im not sure what to do there.


    Quote Originally Posted by MarMan View Post
    That is one formula. Although it doesn';t make sense to me because you ask to calculate points, so I would expect to see total points to the left of the equal sign, not total number of books unless I misunderstand you. And if I do understand you, then you will either need a very complex formula (which I do not recommend because complex formulas are a poor choice because of maintenance) or 3 formulas since you have 3 different computations.
    The textbox for the number of books read is txtNumberOfBooksRead so I declared intTotalNumberOfBooks As Integer and
    intTotalNumberOfBooks = Integer.Parse(txtNumberOfBooks.Text) to convert it.

    Hope I explained it a little better and thank you.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,266

    Re: Beginner Issues with Calculations

    See post #4. You are over complicating this.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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

    Re: Beginner Issues with Calculations

    i'd use the Select Case idea:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim numberOfBooks As Integer = 8
    5.         Dim booksValue As Integer = 0
    6.         Select Case numberOfBooks
    7.             Case 1 To 3
    8.                 booksValue = numberOfBooks * 10
    9.             Case 4 To 6
    10.                 booksValue = 30 + (numberOfBooks - 3) * 15
    11.             Case Is > 6
    12.                 booksValue = 75 + (numberOfBooks - 6) * 20
    13.         End Select
    14.         Label1.Text = booksValue.ToString
    15.     End Sub
    16.  
    17. End Class

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    3

    Re: Beginner Issues with Calculations

    Thanks everyone. I wont be starting loops for another 2 chapters, seems interesting though. I understood the Select Case, thank you.

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,266

    Re: [RESOLVED] Beginner Issues with Calculations

    Code:
            If booksRead > 0 Then
                For ct As Integer = 1 To booksRead
                    points += 10
                    If ct >= 4 Then points += 5
                    If ct > 6 Then points += 5
                Next
            End If
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: [RESOLVED] Beginner Issues with Calculations

    I liked the compactness of the For loop. That is how I would've wrote it years ago. But I've learned that compactness is not necessarily better (well maybe for a mobile device), although for a homework assignment it doesn't matter. The Select Case is much more readable. And it makes maintenance cheaper, the biggest cost for software. Look at the For loop in a year then look at the Select Case. See which one is easier to modify. Then imagine working with hundreds of those functions. You can literally shave hours off your upgrades. I know, I've done it both ways.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,266

    Re: [RESOLVED] Beginner Issues with Calculations

    Quote Originally Posted by MarMan View Post
    I liked the compactness of the For loop. That is how I would've wrote it years ago. But I've learned that compactness is not necessarily better (well maybe for a mobile device), although for a homework assignment it doesn't matter. The Select Case is much more readable. And it makes maintenance cheaper, the biggest cost for software. Look at the For loop in a year then look at the Select Case. See which one is easier to modify. Then imagine working with hundreds of those functions. You can literally shave hours off your upgrades. I know, I've done it both ways.
    I like the select case better also, and the code is faster, in most cases, doing it that way.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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