Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Plz help me...

dee_deedee_dee Member Posts: 3
Hello all i am fairly new to VB and am having some difficulty, I am writting a program that will get an integer from the user and sum the number 1 through that number entered. I know I need a do while or loop but, it wont sum for me any help would be GREATLY appreciated!!!!! This is what I have so far....

Dim intCount As Integer 'Loop counter
Dim strUserInput As String ' To get the user input
Dim total As Integer
Dim sngSum As Single 'Sum of the numbers

'Store the correct value in teh counter and accumulator
intCount = 1
total = 0

'The following loop will get the integer and sum the amount
Do While intCount = 1

strUserInput = InputBox("Enter a postive integer value.", "Input Needed", "10")
If strUserInput <> String.Empty Then
total += intCount
'increment the counter

'Calculate and display the sum
sngSum = total + intCount

MessageBox.Show("The sum of the numbers 1 through " & sngSum & " is " & total.ToString())

Exit Do 'Exit the Loop
End If


End Sub


  • dee_deedee_dee Member Posts: 3
    Is there anyone that can help me with this?!?!?!?!?!
  • DaiMitnickDaiMitnick Member Posts: 77
    Don't log on often so I'm probably too late, but if you still need help then use something like this:

    [code] Dim EndNum As Double 'The last number to add (user's input)
    Dim MyNum As Double 'Number to increase and add to total
    Dim TotalNum As Double 'Total sum

    On Error GoTo Chamone 'Exits if no number or a letter is typed
    EndNum = InputBox("Enter a postive integer value.", "Input Needed", "10") 'Gets user input number
    TotalNum = 0 'The sum, starts at 0
    MyNum = 1 'Start at 1
    Do 'Begin Loop
    TotalNum += MyNum 'Add the current number to the total
    MyNum += 1 'Increase the current number by one
    Loop Until MyNum > EndNum 'End loop when number to add is greater than input number
    MsgBox("The sum of the numbers 1 through " & EndNum & " is " & TotalNum.ToString())
    Exit Sub 'If successful, exits here
    Chamone: 'Label used for when input is empty or invalid (letters typed etc.)
    MsgBox("Error, please type a valid number")
    End Sub

    I suspect your problem was because you were using a string to get the input number, maybe not, doesn't matter, I know this code works. Dai.

    Do or do not, there is no try. |
  • dee_deedee_dee Member Posts: 3
    Thanks for your help, I was able to get it figured out while waiting for a reply :)
Sign In or Register to comment.