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

# Thread: [RESOLVED] Beginner Issues with Calculations

1. ## [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.

2. ## Re: Beginner Issues with Calculations

It's not much difficult.

3. ## 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.

Originally Posted by Soron358
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.

Originally Posted by Soron358
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.

Originally Posted by Soron358
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.

4. ## 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

5. ## Re: Beginner Issues with Calculations

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

Originally Posted by MarMan
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.

Originally Posted by MarMan
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

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

Originally Posted by MarMan
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. ## Re: Beginner Issues with Calculations

See post #4. You are over complicating this.

7. ## Re: Beginner Issues with Calculations

i'd use the Select Case idea:

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

8. ## 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. ## 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```

10. ## 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.

11. ## Re: [RESOLVED] Beginner Issues with Calculations

Originally Posted by MarMan
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.

#### 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