|
-
Sep 24th, 2012, 01:14 PM
#1
Thread Starter
New Member
Request for help
I am working on an assignment due this Friday ( my teacher has told that he can not help me ) and have some sort of logic error. I am trying to create a very basic program that ask the user to enter a positive integer value and give back the sum of 1 to the defined value. example --> user enters 10 & the program gives the answer of 55 ( the sum of all integer between 1 & 10)
Code:
Dim intCounter As Integer
Dim intNumber As Integer
Dim intNumberSum As Integer
Dim Numeric As Integer = 1
Dim strInputBox As String
strInputBox = InputBox("Please enter a positive integer value", "Input needed")
intNumber = CInt(strInputBox)
If strInputBox < 1 Then
MessageBox.Show("Value can not be less then 1")
End If
Do While intCounter < intNumber
intNumberSum = intCounter + 1
intCounter += 1
Numeric = +1
' last idea tried to get the logic work. I don't think this line is really needed. My line of thinking is the counter should be used to get the new value and add one to it
Loop
MessageBox.Show("The sum of the numbers 1 through " & strInputBox & " is " & intNumberSum & ".")
End Sub
-
Sep 24th, 2012, 01:18 PM
#2
Re: Request for help
intNumberSum = intCounter + 1
Just think about what this line actually does for 5 seconds, then think about what it should do. Then read my signature!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Sep 24th, 2012, 01:36 PM
#3
Hyperactive Member
Re: Request for help
I'll give you a hint.....
vb.net Code:
Public Function SumOfIntegers(ByVal int As Integer) As Integer
Dim sum As New Integer
For i = 1 To int
'Code Goes Here...
Next
Return sum
End Function
Now the real question is, how can I make it so I can enter a value of over 65535 and not get an error.....
Last edited by thebuffalo; Sep 24th, 2012 at 01:41 PM.
-
Sep 24th, 2012, 09:02 PM
#4
Thread Starter
New Member
Re: Request for help
Your hint is a bit over my head as this my first time dealing with programming. Would you please break it down a bit and add a line or two about why it is done that way. 
 Originally Posted by thebuffalo
I'll give you a hint.....
vb.net Code:
Public Function SumOfIntegers(ByVal int As Integer) As Integer
Dim sum As New Integer
For i = 1 To int
'Code Goes Here...
Next
Return sum
End Function
Now the real question is, how can I make it so I can enter a value of over 65535 and not get an error.....
-
Sep 24th, 2012, 09:08 PM
#5
Thread Starter
New Member
Re: Request for help
as I am very new to programming and having a learning disability I would to like to ask for a bit more detail as this is rocket science to me.
-
Sep 25th, 2012, 08:40 AM
#6
Hyperactive Member
Re: Request for help
vb.net Code:
Public Function SumOfIntegers(ByVal int As Integer) As Integer 'You do it this way because a function returns a value, your answer, and you can easily put your integer from the textbox into the function.
Dim sum As New Integer ' This is your total sum
For i = 1 To int ' For every number from 1 to your integer, i = that number, this is a loop that will happen for each "i"
'This is the only line of code you need, think about your question.... you take 1, then add 2, then add 3, the sum would be the previous number before adding the next number which would be "i"
sum += i
Next
Return sum ' This returns your final value
End Function
' To call the function you will have to do this... Most Likely in a button.click event that will be below your text box.
Textbox1.Text = SumOfIntegers(Textbox1.Text)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|