Sum of Numbers

Need third messagebox to display info

Page 1 of 1

3 Replies - 24835 Views - Last Post: 16 March 2007 - 01:09 PM Rate Topic: -----

#1 VBisKillingMe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • 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)

Was This Post Helpful? 0
  • +
  • -

#3 VBisKillingMe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1