# Sum of Numbers

Page 1 of 1

## 3 Replies - 33887 Views - Last Post: 16 March 2007 - 01:09 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=25148&amp;s=fecf3e68d9f4eb24994b9d79f6732c52&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 VBisKillingMe

Reputation: 0
• Posts: 11
• Joined: 08-March 07

# Sum of Numbers

Posted 16 March 2007 - 10:13 AM

```Dim strUserInput
Dim IntUserInput As Integer
Dim i As Integer
strUserInput = InputBox("Enter a positive integer value", "Input Needed", 10)
If strUserInput <> String.Empty Then
Try
intUserInput = CInt(strUserInput)
If IntUserInput < 0 Then
MessageBox.Show("Negative numbers are not accepted.")
End If
Catch ex As Exception
MessageBox.Show("Quantity amount must be numeric.")
End Try
'Show messagebox,
Else
'IntUserInput = (IntUserInput + i)
InputBox(MessageBox.Show("Sum of Numbers", "The sum of numbers" & (i + IntUserInput), MessageBoxButtons.OK))
End If
```

Is This A Good Question/Topic? 0

## Replies To: Sum of Numbers

### #2 Jayman

• Student of Life

Reputation: 419
• Posts: 9,532
• Joined: 26-December 05

## Re: Sum of Numbers

Posted 16 March 2007 - 10:55 AM

```InputBox(MessageBox.Show("Sum of Numbers", "The sum of numbers" & (i + IntUserInput), MessageBoxButtons.OK))

```

Remove InputBox from around the MessageBox.Show.
```MessageBox.Show("Sum of Numbers", "The sum of numbers" & (i + IntUserInput), MessageBoxButtons.OK)
```

### #3 VBisKillingMe

Reputation: 0
• Posts: 11
• Joined: 08-March 07

## Re: Sum of Numbers

Posted 16 March 2007 - 12:02 PM

I removed the Input box... I had added that to see if it would help make the Messagebox appear, but it didn't

It's supposed to look like this... application called Sum of Numberfs has two buttons on it... Enter Numbers and Exit. When you click Enter Numbers you get an Inputbox box where a user can enter any positive integer. The default number displays as 10. That part works. The input box is has two buttons OK and Cancel. When you click the OK button you're supposed to a Messagebox(?) that totals every positive integer from 1 to the number that user has input and gives a total. For example, if the input is 10 then the Messagebox displays "The sum of numbers 1 through 10 is 55.

Any ideas?

### #4 Jayman

• Student of Life

Reputation: 419
• Posts: 9,532
• Joined: 26-December 05

## Re: Sum of Numbers

Posted 16 March 2007 - 01:09 PM

That is because the only way the code for that MessageBox will ever execute is if the strUserInput is empty. You currently have it in the Else portion of an IF statement. So only one or the other set of statements will execute depending on the resulting condition of the IF statement.

I think what you mean to do is this:
```Dim strUserInput
Dim IntUserInput As Integer
Dim i As Integer
strUserInput = InputBox("Enter a positive integer value", "Input Needed", 10)
If strUserInput <> String.Empty Then
Try
intUserInput = CInt(strUserInput)
If IntUserInput < 0 Then
MessageBox.Show("Negative numbers are not accepted.")
Exit Sub
End If

'IntUserInput = (IntUserInput + i)
MessageBox.Show("Sum of Numbers", "The sum of numbers" & (i + IntUserInput), MessageBoxButtons.OK)

Catch ex As Exception
MessageBox.Show("Quantity amount must be numeric.")
End Try
'Show messagebox,
Else
MessageBox.Show("Incorrect input", "You must enter a positive integer value", MessageBoxButtons.OK)
Exit Sub
End If
```

You are still missing one important element in order for you to correctly sum up the numbers. You will need a loop to go through all the numbers adding them up as it goes along keeping a running total.