lost with how to do a loop

sum of numbers where do i put the loop and what do i need im so frustr

Page 1 of 1

12 Replies - 3881 Views - Last Post: 10 November 2007 - 06:00 PM Rate Topic: -----

#1 confuzed2much  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-October 07

lost with how to do a loop

Posted 10 October 2007 - 12:15 PM

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



*Always use code blocks please, and put an explanation of your problem in the post not just the title

This post has been edited by PsychoCoder: 10 October 2007 - 12:17 PM


Is This A Good Question/Topic? 0
  • +

Replies To: lost with how to do a loop

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: lost with how to do a loop

Posted 10 October 2007 - 12:18 PM

I need you to explain what you need help with, don't just expect us to decipher the title of your post. Also, this is a VB.Net question so I'm moving it to that forum.

This post has been edited by PsychoCoder: 10 October 2007 - 12:19 PM

Was This Post Helpful? 0
  • +
  • -

#3 confuzed2much  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-October 07

Re: lost with how to do a loop

Posted 10 October 2007 - 12:25 PM

View PostPsychoCoder, on 10 Oct, 2007 - 12:18 PM, said:

I need you to explain what you need help with, don't just expect us to decipher the title of your post. Also, this is a VB.Net question so I'm moving it to that forum.



ok what i am trying to do is get it to display the sum of numbers somehow
all i have is the sum of numbers and the rest is blank on the messagebox.
I just dont understand how to format a loop to add the number entered into the 1st input box and get the result of that number all the way down to 1 added together
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: lost with how to do a loop

Posted 10 October 2007 - 12:50 PM

First, you need to do some reading on Looping Structures as that is one of the most basic programming task.

Second, the loop in the following code is what I believe you're looking for. Read the comments just below the loop

Public Sub Test()
		Dim strUserInput
		Dim IntUserInput As Integer
		Dim i As Integer
		Dim total As Integer = 0
		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
				Else
					for cnt as Integer = 1 to ctype(struserinput)
						total += cnt
					Next
				End If
				'this will display the total of what the user entered
				'if the user entered 10 it will add
				'1 + 2 + 3 + 4 + 5 + 6.....
				'Is that what you're looking for?
				MessageBox.Show(total)
				'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
	End Sub


Was This Post Helpful? 0
  • +
  • -

#5 confuzed2much  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-October 07

Re: lost with how to do a loop

Posted 10 October 2007 - 12:54 PM

yes that is what i am wanting to do i just dont get how to do the loops
sorry i am new to this and just trying stuff from a book i bought
it just helps me to see the code written out once so i have a general idea
Was This Post Helpful? 0
  • +
  • -

#6 confuzed2much  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-October 07

Re: lost with how to do a loop

Posted 10 October 2007 - 01:01 PM

one question what does the public sub test do?
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: lost with how to do a loop

Posted 10 October 2007 - 01:50 PM

You would call the sub from a button click event, like

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	Test()
End Sub



That would call the sub procedure I created.
Was This Post Helpful? 0
  • +
  • -

#8 confuzed2much  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-October 07

Re: lost with how to do a loop

Posted 10 October 2007 - 01:56 PM

I changed the code up a bit and it now looks like this
only problem is is the messagebox that pops up displaying the total i want formatted like this
Title is sum of numbers
and i want it to display the sum of numbers 1 through (the users entered number) is (answer)
here is how i have it set up and it doesnt work

 Dim strUserInput
		Dim IntUserInput As Integer
		Dim total As Integer = 0
		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
				Else
					For cnt As Integer = 1 To CType(strUserInput, Integer)
						total += cnt
					Next
				End If
				'this will display the total of what the user entered
				'if the user entered 10 it will add
				'1 + 2 + 3 + 4 + 5 + 6.....

				MessageBox.Show("Sum of Numbers 1 through " & (strUserInput)("is") & total, "Sum of Numbers")
				
			Catch ex As Exception
				MessageBox.Show("Quantity amount must be numeric.")
			End Try

		Else
			MessageBox.Show("Incorrect input", "You must enter a positive integer value", MessageBoxButtons.OK)
			Exit Sub
		End If
	End Sub


Was This Post Helpful? 0
  • +
  • -

#9 aceofspades686  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 6
  • View blog
  • Posts: 334
  • Joined: 08-October 07

Re: lost with how to do a loop

Posted 10 October 2007 - 02:08 PM

If you're messagebox is the only problem right now, this may help.

MessageBox.Show("Sum of Numbers 1 through " & strUserInput & " is " & total.tostring, "Sum of Numbers")
Was This Post Helpful? 0
  • +
  • -

#10 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: lost with how to do a loop

Posted 10 October 2007 - 02:13 PM

No offense but do you want us to do all your homework for you?
Was This Post Helpful? 0
  • +
  • -

#11 confuzed2much  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-October 07

Re: lost with how to do a loop

Posted 10 October 2007 - 02:20 PM

View PostPsychoCoder, on 10 Oct, 2007 - 02:13 PM, said:

No offense but do you want us to do all your homework for you?

Actually its not really homework
im not taking classes or anything
just got a book and a cd thing
but no i dont expect you to do it all
i am just really slow at learning this
Was This Post Helpful? 0
  • +
  • -

#12 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: lost with how to do a loop

Posted 10 October 2007 - 03:14 PM

View Postconfuzed2much, on 10 Oct, 2007 - 02:20 PM, said:

Actually its not really homework
im not taking classes or anything
just got a book and a cd thing
but no i dont expect you to do it all
i am just really slow at learning this


Once again, I'm not trying to be offensive but you're not going to learn much if you keep asking people to just hand you the code for your solution, that isn't learning or accomplishing anything. I was nice enough to show you how to perform a loop, even gave you link with more information and samples of loops, then you ask someone else for code, without even researching it or attempting to solve it yourself first.
Was This Post Helpful? 0
  • +
  • -

#13 ferrari12508  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3
  • View blog
  • Posts: 1,115
  • Joined: 02-November 07

Re: lost with how to do a loop

Posted 10 November 2007 - 06:00 PM

actually, doing new code is hard for me. But once i have seen how to do something i can remember that. When on the spot i can not come up for new code to save my life
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1