6 Replies - 346 Views - Last Post: 19 February 2012 - 11:02 PM Rate Topic: -----

#1 Failor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-September 09

[Help/Assistance] VB loops

Posted 19 February 2012 - 07:00 PM

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:
Module Module1
    Const MAX_Names = 6
    Sub Main()
        Dim name As String
        Dim count As Integer
        Dim lowName As String
        Dim highName As String

        count = 0

        Do Until count = MAX_Names
            Console.WriteLine("Enter the name: ")
            name = Console.ReadLine

            'Need a condition that desides whish name is low and which high
            'this is where I am stuck

            count = count + 1
        Loop

        Console.Write("Low name is:" & lowName)
        Console.Write("High name is:" & highName)
        
        Console.ReadLine()
    End Sub
End Module


I also attached the chart that I had to follow. If you can help me out or at least point me in a right direction I would really appreciate it. Thank you.

Chart

Attached File(s)

  • Attached File  Chart.pdf (65.09K)
    Number of downloads: 27
  • Attached File  Chart.pdf (65.09K)
    Number of downloads: 24


Is This A Good Question/Topic? 0
  • +

Replies To: [Help/Assistance] VB loops

#2 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: [Help/Assistance] VB loops

Posted 19 February 2012 - 08:53 PM

wouldn't a For Loop be easier for your job? just a thought.
Was This Post Helpful? 0
  • +
  • -

#3 Failor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-September 09

Re: [Help/Assistance] VB loops

Posted 19 February 2012 - 08:57 PM

haven't tried but maybe you can tell whats wrong here and how to correct it. Code work and does what it supposed to do but only if I have the "highName > 0" statement on the bottom. Here is the code:
 Module Module1
    Const MAX_Names = 6
    Sub Main()
        Dim name As String
        Dim lowName As String
        Dim highName As String
        Dim count As Integer

        count = 0
        Do Until count = MAX_Names
            Console.WriteLine("Enter the name: ")
            name = Console.ReadLine

            If name > 0 Then
                name = highName
            Else : name = lowName
            End If

            count = count + 1
        Loop
   --->>>>     highName > 0 <<<<-----

        Console.Write("High name is:" + highName + vbCrLf)
        Console.Write("Low name is:" + lowName + vbCrLf)
        
    End Sub
End Module

Was This Post Helpful? 0
  • +
  • -

#4 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: [Help/Assistance] VB loops

Posted 19 February 2012 - 09:14 PM

I have a small question. How can you compare the variable name which is a string with 0?
Was This Post Helpful? 0
  • +
  • -

#5 Failor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 03-September 09

Re: [Help/Assistance] VB loops

Posted 19 February 2012 - 09:17 PM

I guess I cant...that's why I need help. I am so lost that I try every option that I can possibly think of
Was This Post Helpful? 0
  • +
  • -

#6 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: [Help/Assistance] VB loops

Posted 19 February 2012 - 09:38 PM

I dunno here. It's not like you can set a value to be greater than 0 like that, it's just for comparisons.
Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: [Help/Assistance] VB loops

Posted 19 February 2012 - 11:02 PM

I dont get what you are trying to do here, but let me point to some logic problems here:
            If name > 0 Then 'Tell what are you trying to compare here?
	                name = highName 'this should be reversed: highName = name
	            Else : name = lowName 'also this should be: lowName = name
	            End If ' that gives a little logic here


Now coming back to the first line If name > 0 Then, according to your first comment:
	            'Need a condition that desides whish name is low and which high

I assume you need to know which word is long and which one is small, then the comparison should be about their length. Check to see the length of the name entered if it greater than the highName, then set it to highName, something like:
If highName.Length < name Then
   highName = name

This will work since first we initialize highName = "", which will have length 0.

Now going to the lowName, here a trick is needed, if count = 0, assign name to lowName then for next iterations check for less than that because if you start with the first word to check, always a 0 length word will be smaller
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1