PDA

View Full Version : Help, Visual Basics



Kasi
09-08-2011, 10:18 PM
i am learning about visual basics in computing. ah it's a really simple task (seems it) but i cant get my mind around it. basically i have to create a console application which takes in 4 numbers and adds them up and finds an avarage.
could anyone help me? iv been at it for about a hour now =S

Brandon
09-08-2011, 10:35 PM
File-> New Project -> Console Application:



Module Module1
Sub Main()
Console.WriteLine("Enter A Number")
Dim X As Decimal
Dim Check As VariantType
Try
Check = Console.ReadLine()
If IsNumeric(Check) Then
X = Check
End If
Dim Y As Decimal
Console.WriteLine("Enter A Second Number")
Check = Console.ReadLine()
If IsNumber(Check) Then
Y = Check
End If

Catch e As Exception
Console.WriteLine(vbNewLine & "Enter A Number ONLY!!" & vbNewLine & vbNewLine)
Console.WriteLine(e.ToString)
Console.WriteLine(vbNewLine & vbNewLine & "Please Press any key to continue!")
Console.ReadKey()
End Try
End Sub

End Module
I think thats how u do it... Not sure. Don't program in vb and I don't got it installed currently.
Edit: Will go test it now.. Got visual studio 2010 ultimate.. hope it works :P

Edit2: that works.. Guess I skipped the hello world lol.. thats the first vb program I ever wrote :P Oh and you can just add in the other numbers.. My example only gave the user the chance to enter 2 numbers.. you can easily add more integers and add all four of them.. also try adding a check to see if the input is not a number.

Kasi
09-08-2011, 10:38 PM
ah yer but i have to do all 4 numbers at once -.-
Edit : so basically i enter them like - 1, 2, 3, 4 and then it finds a average
edit2 : yer i got that far but im having trouble spliting the string and finding the numbers everytime i try something i get a error -.-

Brandon
09-08-2011, 11:02 PM
ah yer but i have to do all 4 numbers at once -.-
Edit : so basically i enter them like - 1, 2, 3, 4 and then it finds a average
edit2 : yer i got that far but im having trouble spliting the string and finding the numbers everytime i try something i get a error -.-

Ohh but how are the numbers separated? By a space? A comma? what? Also are negatives involved?

Kasi
09-08-2011, 11:15 PM
yes a ,[space] ah thanks for your help been trying too google it o.0 iv had a little luck with Split but idno quite how to use it

Brandon
09-08-2011, 11:29 PM
yes a ,[space] ah thanks for your help been trying too google it o.0 iv had a little luck with Split but idno quite how to use it


Try this lemmie know if it works.. if you need more of an explanation of it.. I'll try though I don't even know vb at all! I will do my best to explain it how I used it.


Module Module1
Sub Main()
Console.WriteLine("Enter A Number.. Separate Each Number By With A Space")
Dim A As Decimal
Dim Check As String
Dim B As Decimal
Dim C As Decimal
Dim D As Decimal
Try
Check = Console.ReadLine()
Dim CheckArray() As String
Dim Count As Integer
Count = 0
CheckArray = Split(Check)
Dim NonEmpty As Integer = -1
For I As Integer = 0 To CheckArray.Length - 1
If CheckArray(I) <> "" Then
NonEmpty += 1
Count += 1
CheckArray(NonEmpty) = CheckArray(I)
End If
Next
ReDim Preserve CheckArray(NonEmpty)

If (CheckArray.Length < 4) Then
Console.WriteLine("Error!!")
End If

For Z As Integer = 0 To CheckArray.Length - 1
If IsNumeric(CheckArray(Z)) Then
A = CInt(CheckArray(0))
B = CInt(CheckArray(1))
C = CInt(CheckArray(2))
D = CInt(CheckArray(3))
Console.WriteLine(A + B + C + D)
Exit For
End If
Next
Catch ex As Exception
Console.WriteLine(vbNewLine & "You have Entered Less than 4 Numbers OR You Entered A Character that is NOT A Number. Try Again!!" & vbNewLine & vbNewLine)
Console.WriteLine(ex.ToString) 'Remove this To Remove The Debug Printout Of Errors.
End Try
Console.WriteLine(vbNewLine & vbNewLine & "Please Press any key to continue!")
Console.ReadKey()
End Sub

End Module


Edit: Added Error Checking and re-arranged the previous code to stop the console from closing on the correct result.

Kasi
09-08-2011, 11:36 PM
ah yer dude thanks so much it works =D lemme study your code =] <3

Brandon
09-08-2011, 11:38 PM
ah yer dude thanks so much it works =D lemme study your code =] <3

I just edited it.. U might have a bug if u tried the previous one. It only works for "4" numbers.. I don't know how to make it add any amount entered.. but I'm sure you can figure it out faster than me :P

Kasi
09-08-2011, 11:45 PM
ah =] thanks anyways =]