CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2012
    Posts
    2

    [Help] Visula Basic Loops

    I have been trying to figure out a problem for one of my assignments and I got completely stuck. I need to make a program that asks a user for 6 names. After the final name gets entered the program should display 2 names in alphabetical order the highest first the lowest second. Here is what I got so far:
    Code:
    Module Module1
        Const MAX_Names = 6
    
        Sub Main()
    
            Dim name, highName, lowName As String
            Dim count As Integer
    
            count = 0
    
            Console.WriteLine("Enter the name: ")
            name = Console.ReadLine()
    
            Do While count = MAX_Names
                
                If name < lowName Then
                    lowName = name
                ElseIf name > highName Then
                    highName = name
                End If
            Loop
    
            Console.WriteLine("Low name is: " & lowName)
            Console.WriteLine("Low name is: " & highName)
        End Sub
        
    End Module
    If you can help me out or at least point me in a right direction I would really appreciate it. Thank you.
    Last edited by Cimperiali; February 20th, 2012 at 08:26 AM. Reason: added [code][/code] tags

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: [Help] Visula Basic Loops

    First of all your Dim statements are not form properly.
    Code:
    Dim name, highName, lowName As String
    is the same as it you had typed
    Code:
    Dim name as Variant, highName as Variant, lowName As String
    If you want to collect 6 names then you need to loop the input in order to collect the 6

    Your actual loop is formed badly as well

    While Count=Max_Names will never be true so the loop will not execute. E.g. same as saying while 0=6

    Move your do while to the line above the first Console.WriteLine
    Change the while condition to Count<Max_Names
    Or optionally change the while to Until
    Add within the loop code to increase the count e.g. Count+=1
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Feb 2012
    Posts
    2

    Re: [Help] Visula Basic Loops

    Ok I did as much correction as I could can you see whats wrong in here
    Code:
        Module Module1
        Const MAX_Names = 6
    
    
        Sub Main()
            Dim name As String
            Dim count As Integer
            Dim highName As String
            Dim lowName As String
    
            count = 0
            Do Until count = MAX_Names
                Console.WriteLine("Enter the name: ")
                name = Console.ReadLine
    
    
                If name > lowName Then
                    name = lowName
                ElseIf name < highName Then
                    name = highName
                End If
                count = count + 1
            Loop
    
            Console.Write("High name is:" & highName)
            Console.Write("Low name is:" & lowName)
            
            Console.ReadLine()
        End Sub
    End Module
    Last edited by Cimperiali; February 20th, 2012 at 08:25 AM. Reason: Added [code][/code] tags

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: [Help] Visula Basic Loops

    Code:
    If name > lowName Then
    name = lowName
    ElseIf name < highName Then
    name = highName
    End If
    Basically you are never setting lowname or highname and when the name entered is <> then you are setting it to one of these which have not been set so in effect you are setting name=nothing or empty.

    You need to be setting high and low name = to what was entered rather than what is there now.

    You also need to initialize the variables. lowName needs to be initialized high and higname needs to initialized low before you can get the results you expect. You will want the first name they enter to be both the low and the high and then for these to change as needed based on user input.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: [Help] Visula Basic Loops

    Oooppssss....

    Quote Originally Posted by DataMiser View Post
    First of all your Dim statements are not form properly.
    Code:
    Dim name, highName, lowName As String
    is the same as it you had typed
    Code:
    Dim name as Variant, highName as Variant, lowName As String
    That was vb6. In Net, the declaration is correct, and name, highName, lowName are all of String type.

    framework 3.5 and vs 2008: look for "Different Types. You can specify different data types for different variables by using"
    http://msdn.microsoft.com/en-us/libr...(v=vs.90).aspx
    framework 4.0 and vs 2010: look for "Declaring Multiple Variables"
    http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx
    framework 2.0-3.0 and vs 2005 : look for "Multiple Variables. "
    http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx
    Last edited by Cimperiali; February 20th, 2012 at 07:46 AM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: [Help] Visula Basic Loops

    There are many ways you can achieve your goal. Here a couple of examples on how you could code it. :
    Code:
    Module Module1
    
        Sub Main()
            '
            'I need to make a program that asks a user for 6 names. 
            'After the final name gets entered the program should display 2 names in alphabetical order 
            'the highest first the lowest second. 
            '
            'Do you mean you want the highest of the six and the lowest of teh six?
            '
            'you can do it in more than one way:
            '1)using list of strings 
            Const MaxNames As Integer = 6
            Dim namesList As New List(Of String)
            Console.WriteLine("You will be asked to Enter " & MaxNames.ToString() & " names.")
            For counter As Integer = 1 To MaxNames
                Console.WriteLine("Enter name number " & counter.ToString() & " and press Enter")
                namesList.Add(Console.ReadLine())
    
            Next
            'now get the highest and the lowest
           'a small trick: sort the list (or the array) and get the first and last value:
            namesList.Sort()
            Console.WriteLine("Higest name is : " & namesList(MaxNames - 1))
            Console.WriteLine("Lowest name is : " & namesList(0))
            Console.WriteLine("Press q to quit, any other key to proceed with example")
            Dim response As ConsoleKeyInfo = Console.ReadKey()
            If response.Key = ConsoleKey.Q Then
                Return
            End If
    
    
            '--------------------------------------------
            '2)using array of strings
            Dim namesArray(MaxNames - 1) As String
    
            Console.WriteLine("You will be asked to Enter " & MaxNames.ToString() & " names.")
            For counter As Integer = 0 To MaxNames - 1
                Console.WriteLine("Enter name number " & (counter + 1).ToString() & " and press Enter")
                namesArray(counter) = Console.ReadLine()
    
            Next
            Array.Sort(namesArray)
            Console.WriteLine("Higest name is : " & namesArray(MaxNames - 1))
            Console.WriteLine("Lowest name is : " & namesArray(0))
            Console.WriteLine("Press any key to exit")
    
            response = Console.ReadKey()
            'do nothing, quit in any case
            
    
        End Sub
    
    End Module
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: [Help] Visula Basic Loops

    Quote Originally Posted by Cimperiali View Post
    Oooppssss....


    That was vb6. In Net, the declaration is correct, and name, highName, lowName are all of String type.
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured