# getting the average using input boxes and for next statement

Page 1 of 1

## 9 Replies - 7161 Views - Last Post: 28 May 2008 - 12:15 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=52187&amp;s=f1e32c8b12ca96a04f57960b26b1fa83&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 powellm

Reputation: 0
• Posts: 20
• Joined: 30-March 08

# getting the average using input boxes and for next statement

Posted 15 May 2008 - 08:03 PM

so far i have this for my code i need to use input boxes to receive the number of visitors for 5 days also needing to use a for next statement

Const strTitle As String = "Attendance"
Const strPrompt As String = "Enter the Attendance (-1 to finish):"
Const intSentinel As Integer = -1
Dim intVisitors As Integer

This post has been edited by powellm: 16 May 2008 - 12:49 PM

Is This A Good Question/Topic? 0

## Replies To: getting the average using input boxes and for next statement

### #2 Zhalix

• D.I.C Regular

Reputation: 20
• Posts: 260
• Joined: 07-May 08

## Re: getting the average using input boxes and for next statement

Posted 15 May 2008 - 08:54 PM

Well, assuming you're going to be getting the number of visitors for each individual day, then my suggestion is to create an array to hold the data for each five days, that would make it pretty easy to use a For-Next statement to calculate an average as you could use its counter to check each value of the array.

That's a good hint I think.. doesn't give too much away but points you in a good direction.

This post has been edited by Zhalix: 15 May 2008 - 08:57 PM

### #3 powellm

Reputation: 0
• Posts: 20
• Joined: 30-March 08

## Re: getting the average using input boxes and for next statement

Posted 16 May 2008 - 01:54 PM

Zhalix, on 15 May, 2008 - 08:54 PM, said:

Well, assuming you're going to be getting the number of visitors for each individual day, then my suggestion is to create an array to hold the data for each five days, that would make it pretty easy to use a For-Next statement to calculate an average as you could use its counter to check each value of the array.

That's a good hint I think.. doesn't give too much away but points you in a good direction.

i have not learned about arrays yet can you show me something that is on beginner level

### #4 Zhalix

• D.I.C Regular

Reputation: 20
• Posts: 260
• Joined: 07-May 08

## Re: getting the average using input boxes and for next statement

Posted 16 May 2008 - 07:07 PM

This is what you need right?

```Dim intVisitors As Integer
Dim strVisitors As String

For i = 1 To 5

strVisitors = InputBox("Enter the attendants for day " & i & ":")

If IsNumeric(strVisitors) = True Then
If strVisitors > 0 Then
intVisitors = intVisitors + strVisitors
End If
End If

Next i

lblAverageVisitors.Caption = intVisitors / 5

```

### #5 powellm

Reputation: 0
• Posts: 20
• Joined: 30-March 08

## Re: getting the average using input boxes and for next statement

Posted 17 May 2008 - 09:10 PM

thanks

### #6 powellm

Reputation: 0
• Posts: 20
• Joined: 30-March 08

## Re: getting the average using input boxes and for next statement

Posted 27 May 2008 - 03:56 PM

this is my code so far but i cannot seem to get the calculation for the average right
Private Sub cmdStart_Click()
Dim i As Integer
Dim intAverage As Integer
Dim intVisitors As Integer
Dim intSum As Integer
Dim intCounter As Integer
Dim strVisitors As String

intSum = 0
intCounter = 0

For i = 1 To 5

strVisitors = InputBox("Enter the attendants for days Monday through Friday")

If strVisitors <> "" Then
intSum = intSum + i
intCounter = intCounter + 1
End If
Next i

If intCounter > 0 Then intAverage = intSum / intCounter

lblAverageMessage.Caption = "The average attendance for five days is"
lblAverage.Caption = intAverage

End Sub

### #7 Zhalix

• D.I.C Regular

Reputation: 20
• Posts: 260
• Joined: 07-May 08

## Re: getting the average using input boxes and for next statement

Posted 27 May 2008 - 05:47 PM

```For i = 1 To 5

strVisitors = InputBox("Enter the attendants for days Monday through Friday")

If strVisitors <> "" Then
intSum = intSum + i
intCounter = intCounter + 1
End If
Next i

```

Change this line..
```intSum = intSum + i

```

to
```intSum = intSum + strVisitors

```

That should fix it.
Though I don't see why you changed it so much from my code in the first place since mine did the same thing.

This post has been edited by Zhalix: 27 May 2008 - 05:52 PM

### #8 powellm

Reputation: 0
• Posts: 20
• Joined: 30-March 08

## Re: getting the average using input boxes and for next statement

Posted 27 May 2008 - 09:10 PM

something is not right because it does not evaluate the math correctly

### #9 Zhalix

• D.I.C Regular

Reputation: 20
• Posts: 260
• Joined: 07-May 08

## Re: getting the average using input boxes and for next statement

Posted 28 May 2008 - 12:33 AM

Even after you changed what I said?

Can you describe the problem in more detail?

Only thing I can think of that might not be working correctly is that you're putting the outcome of the equation intSum / intCounter into an integer variable. Those don't hold decimal points. Just change the data type from integer to one that supports decimals.

This post has been edited by Zhalix: 28 May 2008 - 12:41 AM

### #10 powellm

Reputation: 0
• Posts: 20
• Joined: 30-March 08

## Re: getting the average using input boxes and for next statement

Posted 28 May 2008 - 12:15 PM

thank you for helping me the problem was with the variables