4 Replies - 387 Views - Last Post: 09 October 2012 - 01:56 PM Rate Topic: -----

#1 CodyMcCoderson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 08-October 12

My Loop Wont Loop Properly.

Posted 09 October 2012 - 01:21 PM

I have created a nested loop which I believe is the proper way to do what I am doing.
The problem im having is when it asks the first candidates name, it then asks the amount of votes 5 times.
IN SHORT
Suppose to read:
Please enter name of candidate 1
Please enter votes for candidate 1
Please enter name of candidate 2
Please enter votes for candidate 2

Reads like:
Please enter name of candidate 1
Please enter votes for candidate 1
Please enter votes for candidate 2
Please enter votes for candidate 3

    Public Sub candidateArray()
        Dim nameArray(4) As String
        Dim voteArray(4) As Integer

        For nameCounter = 0 To 4
            Console.WriteLine("Please enter candidate #" & nameCounter + 1)
            nameArray(nameCounter) = Console.ReadLine()

            For voteCounter = 0 To 4
                Console.WriteLine("Please enter the number of votes for candidate #" & voteCounter + 1)
                voteArray(voteCounter) = Console.ReadLine()

                Console.WriteLine()
            Next
        Next
     End Sub 
     End Module



Is This A Good Question/Topic? 0
  • +

Replies To: My Loop Wont Loop Properly.

#2 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 485
  • View blog
  • Posts: 3,248
  • Joined: 12-January 10

Re: My Loop Wont Loop Properly.

Posted 09 October 2012 - 01:29 PM

View PostCodyMcCoderson, on 09 October 2012 - 04:21 PM, said:

I have created a nested loop which I believe is the proper way to do what I am doing.
The problem im having is when it asks the first candidates name, it then asks the amount of votes 5 times.
IN SHORT
Suppose to read:
Please enter name of candidate 1
Please enter votes for candidate 1
Please enter name of candidate 2
Please enter votes for candidate 2

Reads like:
Please enter name of candidate 1
Please enter votes for candidate 1
Please enter votes for candidate 2
Please enter votes for candidate 3

    Public Sub candidateArray()
        Dim nameArray(4) As String
        Dim voteArray(4) As Integer

        For nameCounter = 0 To 4
            Console.WriteLine("Please enter candidate #" & nameCounter + 1)
            nameArray(nameCounter) = Console.ReadLine()

            For voteCounter = 0 To 4
                Console.WriteLine("Please enter the number of votes for candidate #" & voteCounter + 1)
                voteArray(voteCounter) = Console.ReadLine()

                Console.WriteLine()
            Next
        Next
     End Sub 
     End Module




i am guessing it is because of this


Console.WriteLine()
Was This Post Helpful? 0
  • +
  • -

#3 CodyMcCoderson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 08-October 12

Re: My Loop Wont Loop Properly.

Posted 09 October 2012 - 01:32 PM

Nope, tried that a while ago.
Thats what I thought.
Was This Post Helpful? 0
  • +
  • -

#4 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: My Loop Wont Loop Properly.

Posted 09 October 2012 - 01:51 PM

The nested loop will loop every time the loop on the outside runs. The nested loop will run 0 to 4 before going back to the outside loop.
You could add an Exit For after Console.WriteLine in the nested loop.
Keep track of the counter:

    Public Sub candidateArray()
        Dim nameArray(4) As String
        Dim voteArray(4) As Integer
        Dim voter As Integer 'keep track of the voter

        For nameCounter = 0 To 4
            Console.WriteLine("Please enter candidate #" & nameCounter + 1)
            nameArray(nameCounter) = Console.ReadLine()

            For voteCounter = voter To 4
                Console.WriteLine("Please enter the number of votes for candidate #" & voteCounter + 1)
                voteArray(voteCounter) = Console.ReadLine()

                Console.WriteLine()
                Exit For 'this will ensure that the loop runs only one time before exiting
            Next
        Next
     End Sub 
     End Module



You may not even need the second For Loop:

    Public Sub candidateArray()
        Dim nameArray(4) As String
        Dim voteArray(4) As Integer

        For nameCounter = 0 To 4
            Console.WriteLine("Please enter candidate #" & nameCounter + 1)
            nameArray(nameCounter) = Console.ReadLine()

            
                Console.WriteLine("Please enter the number of votes for candidate #" & nameCounter + 1)
                voteArray(nameCounter) = Console.ReadLine()

                
            Next
        Next
     End Sub 
     End Module


Give those a try and see what happens.
Was This Post Helpful? 0
  • +
  • -

#5 CodyMcCoderson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 08-October 12

Re: My Loop Wont Loop Properly.

Posted 09 October 2012 - 01:56 PM

I was missing the "Exit for"

Perfect, thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1