# How do I get the average either higher/lower in their textboxes?

• (2 Pages)
• 1
• 2

## 22 Replies - 1760 Views - Last Post: 26 March 2010 - 03:45 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=164291&amp;s=ea74b7285f3426d27c12cf1da6721757&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mbrother64

• D.I.C Head

Reputation: -9
• Posts: 78
• Joined: 30-August 09

# How do I get the average either higher/lower in their textboxes?

Posted 25 March 2010 - 04:31 PM

Heres my code so far:

```Dim Subject As Integer
Subject = InputBox("How many products")
Dim i As Integer
Dim num As Integer
Dim total As Integer
Dim average As Integer
Dim total1 As Integer
Dim total2 As Integer

For i = 1 To Subject
num = (Val(InputBox("Enter sales amount:")))
total = total + num
average = total / Subject
ListBox1.Items.Add(word & ControlChars.Tab & num)
Select Case num
Case Is > 1200
total1 = total1 + num
TextBox3.Text = total1
ListBox2.Items.Add(word & ControlChars.Tab & num)
Case Is <= 1200
ListBox3.Items.Add(word & ControlChars.Tab & num)
total2 = total2 + num
TextBox4.Text = total2
End Select

Next

TextBox1.Text = total
TextBox2.Text = average
```

I want the average above 1200 going in a TextBox5.Text and the average lower than 1200 TextBox6.Text.

I've tried stuff like; "average1 = average1 + average" and "average1 = total1 / subject" and average 1 = total1 / subject1". But none of it appears to be working.

Is This A Good Question/Topic? 0

## Replies To: How do I get the average either higher/lower in their textboxes?

### #2 ZRonZ

• D.I.C Head

Reputation: 28
• Posts: 206
• Joined: 09-January 09

## Re: How do I get the average either higher/lower in their textboxes?

Posted 25 March 2010 - 05:14 PM

The same way you did with the sum. It's not that hard - you already know how to do it.
Was This Post Helpful? 0

### #3 mbrother64

• D.I.C Head

Reputation: -9
• Posts: 78
• Joined: 30-August 09

## Re: How do I get the average either higher/lower in their textboxes?

Posted 25 March 2010 - 08:43 PM

As I said, I've already tried that but it aint working.
Was This Post Helpful? 0

### #4 mbrother64

• D.I.C Head

Reputation: -9
• Posts: 78
• Joined: 30-August 09

## Re: How do I get the average either higher/lower in their textboxes?

Posted 25 March 2010 - 09:32 PM

Want me to spell it out for you? Ive tried:
```Dim Subject As Integer
Subject = InputBox("How many products")
Dim i As Integer
Dim num As Integer
Dim total As Integer
Dim average As Integer
Dim total1 As Integer
Dim total2 As Integer
Dim average1 As Integer ' I tried this
Dim average2 As Integer ' I tried this

For i = 1 To Subject
num = (Val(InputBox("Enter sales amount:")))
total = total + num
average = total / Subject
ListBox1.Items.Add(word & ControlChars.Tab & num)
Select Case num
Case Is > 1200
total1 = total1 + num
average1 = average1 + average ' I tried this
TextBox5.Text = average1 ' I tried this
TextBox3.Text = total1
ListBox2.Items.Add(word & ControlChars.Tab & num)
Case Is <= 1200
ListBox3.Items.Add(word & ControlChars.Tab & num)
total2 = total2 + num
average2 = average2 + average ' I tried this
TextBox6.Text = average2 ' I tried this
TextBox4.Text = total2
End Select

Next

TextBox1.Text = total
TextBox2.Text = average
```

But that didn't work. And then I've tried
```Dim Subject As Integer
Subject = InputBox("How many products")
Dim i As Integer
Dim num As Integer
Dim word As String
Dim total As Integer
Dim average As Integer
Dim total1 As Integer
Dim total2 As Integer
Dim average1 As Integer
Dim average2 As Integer

For i = 1 To Subject
word = (InputBox("Enter product name:"))
num = (Val(InputBox("Enter sales amount:")))
total = total + num
average = total / Subject
ListBox1.Items.Add(word & ControlChars.Tab & num)
Select Case num
Case Is > 1200
total1 = total1 + num
average1 = total1 / Subject ' I tried this
TextBox5.Text = average1
TextBox3.Text = total1
ListBox2.Items.Add(word & ControlChars.Tab & num)
Case Is <= 1200
ListBox3.Items.Add(word & ControlChars.Tab & num)
total2 = total2 + num
average2 = total1 / Subject ' I tried this
TextBox6.Text = average2
TextBox4.Text = total2
End Select

Next

TextBox1.Text = total
TextBox2.Text = average
```

But that dosent work so Ive tried stuff like:
```Dim Subject As Integer
Subject = InputBox("How many products")
Dim i As Integer
Dim num As Integer
Dim total As Integer
Dim average As Integer
Dim total1 As Integer
Dim total2 As Integer
Dim average1 As Integer
Dim average2 As Integer

For i = 1 To Subject
num = (Val(InputBox("Enter sales amount:")))
total = total + num
average = total / Subject
ListBox1.Items.Add(word & ControlChars.Tab & num)
Select Case num
Case Is > 1200
total1 = total1 + num
average1 = total1 / Subject ' I tried this
TextBox5.Text = average1
TextBox3.Text = total1
ListBox2.Items.Add(word & ControlChars.Tab & num)
Case Is <= 1200
ListBox3.Items.Add(word & ControlChars.Tab & num)
total2 = total2 + num
average2 = total1 / Subject ' I tried this
TextBox6.Text = average2
TextBox4.Text = total2
End Select

Next

TextBox1.Text = total
TextBox2.Text = average
```

But that still dosent. And then Ive tried stuff like:
```Dim Subject As Integer
Subject = InputBox("How many products")
Dim i As Integer
Dim num As Integer
Dim total As Integer
Dim average As Integer
Dim total1 As Integer
Dim total2 As Integer
Dim average1 As Integer
Dim average2 As Integer
Dim subject1 As Integer ' I tried this
Dim subject2 As Integer ' I tried this

For i = 1 To Subject
num = (Val(InputBox("Enter sales amount:")))
total = total + num
average = total / Subject
ListBox1.Items.Add(word & ControlChars.Tab & num)
Select Case num
Case Is > 1200
total1 = total1 + num
subject1 = subject1 + Subject
average1 = total1 / subject1 ' I tried this
TextBox5.Text = average1
TextBox3.Text = total1
ListBox2.Items.Add(word & ControlChars.Tab & num)
Case Is <= 1200
ListBox3.Items.Add(word & ControlChars.Tab & num)
total2 = total2 + num
subject2 = subject2 + Subject
average2 = total1 / subject2 ' I tried this
TextBox6.Text = average2
TextBox4.Text = total2
End Select

Next

TextBox1.Text = total
TextBox2.Text = average
```

But that still dosent work. Dont tell me I already know how to do it.

This post has been edited by mbrother64: 26 March 2010 - 12:46 AM

Was This Post Helpful? 0

### #5 AdamSpeight2008

• MrCupOfT

Reputation: 2294
• Posts: 9,531
• Joined: 29-May 08

## Re: How do I get the average either higher/lower in their textboxes?

Posted 25 March 2010 - 09:41 PM

Problem 1
How long is going to take you to understand that the contents of a textbox / Input is not a number, if to you it looks like one but a string of characters. So before treating it as a number, see if can be parsed as one, for example

```Dim strInput As String = InputBox("")
Dim intInput As Integer =0
If Integer.TryParse(strInput, intInput) Then
' The string was number '
Else
' The string is not a number '
EndIf

```

Problem 2

Doing things in vb6 way.
Defining a variable to contain the current value of a loop.

```Dim i As Integer
For i = 0 To 10
Next
```

Just do
```For i As Integer=0 TO 10
Next
```

Problem 3
Not giving your control meaningful name, textbox1 is useless without knowing the form.
Whereas txtUserAge conveys the its the textbox the user inputs there age.

Problem 4

Quote

But that still dosent work. Dont tell me I already know how to do it.

It doesn't work doesn't mean shit to use. Give use details, what you expected it do, what actually happened.
Remember we are not there beside, we can't see your screen.
Was This Post Helpful? 0

### #6 mbrother64

• D.I.C Head

Reputation: -9
• Posts: 78
• Joined: 30-August 09

## Re: How do I get the average either higher/lower in their textboxes?

Posted 25 March 2010 - 10:41 PM

As I said before "I want the average above 1200 going in a TextBox5.Text and the average lower than 1200 in TextBox6.Text. ". I can get the total in TextBox1.Text, the average in TextBox2.Text, the total above 1200 going in TextBox3.Text and the total lower than 1200 into TextBox4.Text but I cant get the average above 1200 going in a TextBox5.Text and the average lower than 1200 in TextBox6.Text.

It either gives me what I want or it dosen't, by doing what I tried: it dosen't. The numbers just come out all wrong.

The average above 1200 in one textbox and the average lower than 1200 in another, some help please.

This post has been edited by mbrother64: 26 March 2010 - 12:41 AM

Was This Post Helpful? 0

### #7 AdamSpeight2008

• MrCupOfT

Reputation: 2294
• Posts: 9,531
• Joined: 29-May 08

## Re: How do I get the average either higher/lower in their textboxes?

Posted 26 March 2010 - 12:39 AM

Stop for a moment and forget about this problem in terms of code.

Write down the steps needed to solve to problem, using simple statements (pseudocode)

I'll get you started

1. Get User Input: Number of Products
2. Loop
3. Get User Input: Product Name
4. Get User Input: Sales Amount
5. If Sales Amount Is Greater Than 1200 Then
6. Add Sales Amount to Total Above
7. Increment Count Above By 1
9. Else
etc

As a task finish the above pseudocode before posting any more code.

This post has been edited by AdamSpeight2008: 26 March 2010 - 12:40 AM
Reason for edit:: Add link to pseudocode tutorial

Was This Post Helpful? 2

### #8 mbrother64

• D.I.C Head

Reputation: -9
• Posts: 78
• Joined: 30-August 09

## Re: How do I get the average either higher/lower in their textboxes?

Posted 26 March 2010 - 01:00 AM

As a task? You want me to do a task for you? No I'm not. The code works fine up until getting the average above 1200 going in a TextBox5.Text and the average lower than 1200 in TextBox6.Text.
Was This Post Helpful? -2

### #9 AdamSpeight2008

• MrCupOfT

Reputation: 2294
• Posts: 9,531
• Joined: 29-May 08

## Re: How do I get the average either higher/lower in their textboxes?

Posted 26 March 2010 - 01:43 AM

The task is more for your benefit than mine, as you are too focused on coding too see the problem.
If the task was pointless I wouldn't suggest it, trust me doing the task will help you in the long run.

Or do you just want someone to give the code to you.
If you do, this is bad for several reasons.
1. You don't learn anything.
2. The person you set the original problem assumes you have the knowledge display in the provided code.
3. Against the spirit of the Dream in Code rules.
If don't then do the Simple Task of writing the pseudocode. (It'll take less than 5 minutes, even for beginner coder.)
Was This Post Helpful? 2

### #10 mbrother64

• D.I.C Head

Reputation: -9
• Posts: 78
• Joined: 30-August 09

## Re: How do I get the average either higher/lower in their textboxes?

Posted 26 March 2010 - 02:14 AM

You've just ruined this whole topic adam, now no one else wants to comment or help. I understand all of my code so far, I know what I'm doing and what I'm trying to accomplish. "too focused on coding too see the problem." Give me a break. I won't learn anything? You dont know that. Theres a specific way on how to achieve specific things. Like how to get the average above 1200 going in TextBox5.Text and the average lower than 1200 in TextBox6.Text. What Ive tried dosen't work, so I'm here to find out what does.

This post has been edited by mbrother64: 26 March 2010 - 02:17 AM

Was This Post Helpful? -2

### #11 JackOfAllTrades

• Saucy!

Reputation: 6237
• Posts: 24,002
• Joined: 23-August 08

## Re: How do I get the average either higher/lower in their textboxes?

Posted 26 March 2010 - 04:54 AM

mbrother64, you have yet to even relay how what you're trying to do doesn't work!!!

As AdamSpeight2008 has said, integers and strings are not freely interchangeable.

```Dim Subject As Integer
Subject = InputBox("How many products")

```

InputBox returns a string, Subject is an integer, so how would this loop:
```For i = 1 To Subject
```

work???
Was This Post Helpful? 0

### #12 ZRonZ

• D.I.C Head

Reputation: 28
• Posts: 206
• Joined: 09-January 09

## Re: How do I get the average either higher/lower in their textboxes?

Posted 26 March 2010 - 07:29 AM

Adam is absolutely correct, you need to do a task. As it stands now, each time you work on a new step in your project you post it without much work done. You can't keep track of your variables and what they represent, you don't get the math right. You want others to do your work. Try programming, you may like it. And programming starts with:

Quote

Write down the steps needed to solve to problem, using simple statements (pseudocode)

1. Get User Input: Number of Products
2. Loop
3. Get User Input: Product Name
4. Get User Input: Sales Amount
5. If Sales Amount Is Greater Than 1200 Then
6. Add Sales Amount to Total Above
7. Increment Count Above By 1
9. Else
etc

Was This Post Helpful? 1

### #13 emresutisna

• D.I.C Head

Reputation: 21
• Posts: 80
• Joined: 14-May 09

## Re: How do I get the average either higher/lower in their textboxes?

Posted 26 March 2010 - 08:26 AM

Just try this code :
```        ListBox1.Items.Clear()
ListBox2.Items.Clear()
ListBox3.Items.Clear()
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox6.Clear()
Dim Subject As Integer
Subject = Val(InputBox("How many products"))
Dim i As Integer
Dim j As Integer = 0
Dim k As Integer = 0
Dim num As Integer
Dim total As Integer
Dim average As Integer
Dim total1 As Integer
Dim total2 As Integer

For i = 1 To Subject
num = (Val(InputBox("Enter sales amount:")))
total = total + num
average = total / Subject
ListBox1.Items.Add(word & ControlChars.Tab & num)
Select Case num
Case Is > 1200
total1 = total1 + num
j += 1
TextBox3.Text = total1
TextBox5.Text = total1 / j
ListBox2.Items.Add(word & ControlChars.Tab & num)
Case Is <= 1200
ListBox3.Items.Add(word & ControlChars.Tab & num)
total2 = total2 + num
k += 1
TextBox4.Text = total2
TextBox6.Text = total2 / k
End Select

Next

TextBox1.Text = total
TextBox2.Text = average

```

Was This Post Helpful? 1

### #14 Luc001

• D.I.C Addict

Reputation: 84
• Posts: 617
• Joined: 04-May 09

## Re: How do I get the average either higher/lower in their textboxes?

Posted 26 March 2010 - 09:07 AM

Hi,

Here's an example how you should do it, included the suggestion from AdamSpeight2008.

```Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
Dim Subject As Integer
Subject = InputBox("How many products")
Dim i As Integer
Dim num As Integer
Dim total As Integer
Dim total1 As Integer
Dim counter As Integer = 0
Dim counter1 As Integer = 0

For i = 1 To Subject
num = (Val(InputBox("Enter sales amount:")))

ListBox1.Items.Add(word & ControlChars.Tab & num)
Select Case num
Case Is > 1200
total1 = total1 + num
TextBox2.Text = total1
If TextBox2.Text = total1 Then
counter1 = counter1 + 1
End If
Label1.Text = counter1
Case Is <= 1200
total = total + num
TextBox3.Text = total
If TextBox3.Text = total Then
counter = counter + 1
End If
Label2.Text = counter
End Select
Next

TextBox1.Text = total + total1
TextBox4.Text = TextBox1.Text / Subject ' calculates the average.

' look here how it should be

If Integer.TryParse(counter, total) Then
TextBox5.Text = TextBox3.Text / counter
End If
If Integer.TryParse(counter1, total1) Then
TextBox6.Text = TextBox2.Text / counter1
End If

End Sub
```

This post has been edited by Luc001: 26 March 2010 - 09:10 AM

Was This Post Helpful? 0

### #15 mbrother64

• D.I.C Head

Reputation: -9
• Posts: 78
• Joined: 30-August 09

## Re: How do I get the average either higher/lower in their textboxes?

Posted 26 March 2010 - 11:39 AM

JackOfAllTrades, on 26 March 2010 - 03:54 AM, said:

mbrother64, you have yet to even relay how what you're trying to do doesn't work!!!

As AdamSpeight2008 has said, integers and strings are not freely interchangeable.

```Dim Subject As Integer
Subject = InputBox("How many products")

```

InputBox returns a string, Subject is an integer, so how would this loop:
```For i = 1 To Subject
```

work???

Actually what I was trying to say before is that it DOES work!!! You just add the Try Catche function to bypass that error. Trust me, It loops around fine and the whole entire code works. (I dont need to waste my time doing pseudocode, are you kidding me?)

All I need now is some little help on how to find out the averages, which shouldn't be more than a few lines of code.
Was This Post Helpful? -1

• (2 Pages)
• 1
• 2

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}