Results 1 to 9 of 9

Thread: Help, Visual Basics

  1. #1
    Join Date
    Dec 2007
    Posts
    2,112
    Mentioned
    71 Post(s)
    Quoted
    580 Post(s)

    Default Help, Visual Basics

    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

  2. #2
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    File-> New Project -> Console Application:

    Code:
    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.
    Last edited by Brandon; 09-08-2011 at 11:03 PM.
    I am Ggzz..
    Hackintosher

  3. #3
    Join Date
    Dec 2007
    Posts
    2,112
    Mentioned
    71 Post(s)
    Quoted
    580 Post(s)

    Default

    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 -.-
    Last edited by Kasi; 09-08-2011 at 10:41 PM.

  4. #4
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by pur3b100d View Post
    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?
    I am Ggzz..
    Hackintosher

  5. #5
    Join Date
    Dec 2007
    Posts
    2,112
    Mentioned
    71 Post(s)
    Quoted
    580 Post(s)

    Default

    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

  6. #6
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by pur3b100d View Post
    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.
    Code:
    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.
    Last edited by Brandon; 09-08-2011 at 11:37 PM.
    I am Ggzz..
    Hackintosher

  7. #7
    Join Date
    Dec 2007
    Posts
    2,112
    Mentioned
    71 Post(s)
    Quoted
    580 Post(s)

    Default

    ah yer dude thanks so much it works =D lemme study your code =] <3

  8. #8
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by pur3b100d View Post
    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
    I am Ggzz..
    Hackintosher

  9. #9
    Join Date
    Dec 2007
    Posts
    2,112
    Mentioned
    71 Post(s)
    Quoted
    580 Post(s)

    Default

    ah =] thanks anyways =]

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •