# Program to calculate averages from user input fails to run

This is the code I have so far and it's not running at all.

``````    Module Average
Sub Main()
Dim v As Integer
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim s As Integer
Console.WriteLine("How Many values are there?")
If v = 4 Then
Console.WriteLine("Averaging values")
s = 1
For s = 1 To 1
s = a + b + c + d
s = s / v
Console.Writeline(s)
Else
Console.WriteLine("HI")
Next s
End If
End Sub
End Module
``````

I do need this for a class. I'm just trying to make it give errors if the user inputs impossible numbers or makes it need to give an error.

If the code were formatted a little more nicely, it might be easier to spot the problem. There's an Else inside the For loop. If i do this it seems to work (though onlinegdb seems a little buggy):

``````Sub Main()
Console.WriteLine("Hello World")
Dim v As Integer
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim s As Integer
Console.WriteLine("How Many values are there?")
If v = 4 Then
Console.WriteLine("Averaging values")
s = 1
For s = 1 To 1
s = a + b + c + d
s = s / v
Console.Writeline(s)
Next s
Else
Console.WriteLine("HI")
End If

End Sub
``````

I really wanted to use `Integer.TryParse` for user input but I was afraid the addition of the List type would be enough to swallow.

``````Sub Main()
Dim lstNumbers As New List(Of Integer)
Dim inputNumber As Integer = 0
Console.WriteLine("Welcome to Number Average")
Console.WriteLine("When you are finished entering your numbers, enter Done.")
Do
Console.WriteLine("Enter a number or Done")
If input.ToUpper = "Done".ToUpper Then
Exit Do
End If
Loop
If lstNumbers.Count > 0 Then
Dim sum As Integer
Dim s As String
For Each i As Integer In lstNumbers
sum += i
s &= i.ToString & ", "
Next
Dim average As Double = sum / lstNumbers.Count
Console.WriteLine(\$"The average of the numbers {s} is {average}")
Else
Console.WriteLine("You didn't enter any numbers.")
End If

End Sub
``````

In C#/VB.Net, we have generic `List`s to store collection of data. In VB.Net, it looks like this : `List(Of DataType_Or_Class)`. As it is particularly an `Integer` case, you can simply create a `List` of `Integer`s as follows :

``````Dim NumberList as New List(Of Integer)
``````

Now, `int16` or `int32` or even `int64`, it's your choice, but using `Integer` itself won't make any difference in your case.

Now comes checking if user has inputted a numeric value or not. This can be achieved in many ways like allowing only numeric inputs or reading the input upon submitting and checking if it is a proper integer or performing an exception check when trying to sum all integers in the `List`. As other answers seem to mention somewhat other ways, here's a sample code of the first way i mentioned :

``````Dim _val As String = ""
Console.Write("Enter a number: ")
Dim key As ConsoleKeyInfo

Do

If key.Key <> ConsoleKey.Backspace Then
Dim val As Integer = 0
Dim _x As Boolean = Integer.TryParse(key.KeyChar.ToString(), val)
'''Integer.Parse would work aswell, but TryParse will take care of exceptions.
If _x Then
_val += key.KeyChar
Console.Write(key.KeyChar)
End If
Else

If key.Key = ConsoleKey.Backspace AndAlso _val.Length > 0 Then
_val = _val.Substring(0, (_val.Length - 1))
Console.Write(vbBack & " " & vbBack)
End If
End If
Loop While key.Key <> ConsoleKey.Enter

Console.WriteLine() '''Calling Console.WriteLine will execute expected results here.
``````

No more codes now. Hope you got the basics. Now, just use a simple logic. Create a list of integers, eliminate all unnecessary keystrokes/key presses except for numeric keys, upon pressing enter (`While key.Key <> ConsoleKey.Enter`), add each value to the list and finally, calculate the average.

For the last step, you can use this code :

``````Dim average As Double = If(list.Count > 0, list.Average(),
``````

It will take care of having no values in list. Adios, amigos.