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 07:26 AM.
Reason: added [code][/code] tags
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
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 07:25 AM.
Reason: Added [code][/code] tags
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.
Last edited by Cimperiali; February 20th, 2012 at 06: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.
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.
Bookmarks