11 Replies - 2509 Views - Last Post: 29 September 2012 - 06:44 PM Rate Topic: -----

#1 tyark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 27-September 12

Help with loops VB.net

Posted 27 September 2012 - 03:38 PM

Hi, I'm having problems with this program. The program is supposed to find the largest and smallest numbers after you enter as many numbers as you want, and the loop ends when you enter -99.

Ive made a program, but I'm having trouble getting it to store a value and compare it with the previous entered value. I would really appreciate if someone could tell me what I would need to add (not code but another if/then or another loop etc.)

The problem is that the results show me the last number I entered, and not the actual smallest and largest values.

I've tried nesting an if / then statement in the subs smallest and largest, but I realized why that wouldn't worked. I'm really stumped, any help is appreciated!

Module Module1

    Sub Main()
        Dim userNum, smallest, largest As Integer

        showintro()

        Do
            Console.Write("Please enter a number: ")
            userNum = CInt(Console.ReadLine())
            If userNum <= -99 Then
                subSmallest(userNum, smallest)
            ElseIf userNum > -99 Then
                subLargest(userNum, largest)
            End If
        Loop Until userNum = -99

        displayResults(smallest, largest)

    End Sub

    Sub showintro()
        Console.writeline("This program will get all of the numebers you enter ")
        console.writeline("and determine the smallest and largest Number")
        Console.writeline("When you are finished inputing your numbers, enter -99")
    End Sub

    Sub subSmallest(ByRef userInput As Integer, ByRef smallestNumber As Integer)
        smallestNumber = userInput
    End Sub

    Sub subLargest(ByRef userNumber As Integer, ByRef largestNumber As Integer)
        largestNumber = userNumber
    End Sub

    Sub displayResults(ByVal smallestNumber As Integer, ByVal largestNumber As Integer)
        Console.WriteLine("The Smallest Number is: " & smallestNumber & " the Largest Number is: " & largestNumber)
    End Sub
End Module



This post has been edited by GunnerInc: 27 September 2012 - 04:37 PM
Reason for edit:: Fixed broken code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Help with loops VB.net

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: Help with loops VB.net

Posted 27 September 2012 - 03:57 PM

You are always assigning user input to smallestNumber and largestNumber, and they are references of smallest and largest variables.

Edit: ... and your idea of if statement in both subs was correct, just compare user input to what you already have in smallest or largest variable.

This post has been edited by lucky3: 27 September 2012 - 04:02 PM

Was This Post Helpful? 1
  • +
  • -

#3 tyark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 27-September 12

Re: Help with loops VB.net

Posted 27 September 2012 - 04:11 PM

oh, I see what you mean. Some ideas are popping into my head now on how to tweak / fix this. I'm going to work on it and see how my ideas work. Thanks for the feedback! :)
Was This Post Helpful? 0
  • +
  • -

#4 tyark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 27-September 12

Re: Help with loops VB.net

Posted 27 September 2012 - 04:18 PM

Edit:
I shouldn't really say my ideas ^^
(sorry I couldnt find the edit button)
Was This Post Helpful? 0
  • +
  • -

#5 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: Help with loops VB.net

Posted 27 September 2012 - 04:30 PM

Good ;)
Post your solution later, if you want to, because there are some things to talk about how it can be done in some other different ways. It's almost 1:30 AM here, so I won't respond for several hours now, but I'm sure someone else might want to say a word or two with you about it.
Was This Post Helpful? 0
  • +
  • -

#6 tyark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 27-September 12

Re: Help with loops VB.net

Posted 27 September 2012 - 04:35 PM

View Postlucky3, on 27 September 2012 - 03:57 PM, said:

You are always assigning user input to smallestNumber and largestNumber, and they are references of smallest and largest variables.

Edit: ... and your idea of if statement in both subs was correct, just compare user input to what you already have in smallest or largest variable.



Oh my gosh, thanks SO much! I just needed to add 6 lines of code (2 if / then's like you said).

Again, I really appreciate you taking the time to steer me in the right direction! :) :) :w00t:
Was This Post Helpful? 0
  • +
  • -

#7 tyark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 27-September 12

Re: Help with loops VB.net

Posted 27 September 2012 - 05:22 PM

So, here is my code, and this is what I'm submitting, but if anyone has any suggestions on how to make the code better I'm always open to learning new methods!
(hopefully I get the code tags right this time :P

Module Module1
    
    Sub Main()
        'Declare my variables
        Dim userNum, smallest, largest As Integer
        'Call a module to show the intro to the program
        showintro()
        'Do Until loop, get input, send input to a certain module
        Do
            Console.Write("Please enter a number: ")
            userNum = CInt(Console.ReadLine())
            If userNum <= -99 Then
                subSmallest(userNum, smallest)
            ElseIf userNum > -99 Then
                subLargest(userNum, largest)
            End If
        Loop Until userNum = -99
        'display results
        displayResults(smallest, largest)
    End Sub

    'This sub shows the intro to the program
    Sub showintro()
        Console.WriteLine("This program will get all of the numbers you enter ")
        console.writeline("and determine the smallest and largest Number")
        Console.WriteLine("When you are finished inputing your numbers, enter -99")
        Console.WriteLine("------------------------------------------------------")
    End Sub

    'This sub determines the smallest value
    Sub subSmallest(ByRef userInput As Integer, ByRef smallestNumber As Integer)
        If userInput < smallestNumber Then
            smallestNumber = userInput
        End If
    End Sub

    'This sub determines the largest value
    Sub subLargest(ByRef userNumber As Integer, ByRef largestNumber As Integer)
        If userNumber > largestNumber Then
            largestNumber = userNumber
        End If
    End Sub

    'This sub displays the results by value
    Sub displayResults(ByVal smallestNumber As Integer, ByVal largestNumber As Integer)
        Console.WriteLine("-----------------------------------------------------")
        Console.WriteLine("The Smallest Number is: " & smallestNumber)
        Console.WriteLine("The Largest Number is: " & largestNumber)
    End Sub

End Module


And here are the results when the program is run.

'This program will get all of the numbers you enter
'and determine the smallest and largest Number
'When you are finished inputing your numbers, enter -99
'------------------------------------------------------
'Please enter a number: 30
'Please enter a number: 5000
'Please enter a number: 0
'Please enter a number: -10
'Please enter a number: -3000
'Please enter a number: -99
'-----------------------------------------------------
'The Smallest Number is: -3000
'The Largest Number is: 5000
'Press any key to continue . . .
Was This Post Helpful? 0
  • +
  • -

#8 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: Help with loops VB.net

Posted 28 September 2012 - 12:30 AM

Great job! Now you have wonderful opportunity to learn more from it.

Here is some information:
  • you can have (but it's not needed - so it's optional) only one method (Sub) to compare and adjust (if needed) the smallest and largest numbers;
  • none of your sub routines needs incoming parameters to compare values/display results;
  • your application breaks, if user enters non integer value.


I suggest (if you have time and will), that you try to find a solution to those 3 bits of information, and after that, compare it to your current solution. Think about what in your current code isn't needed, and why. Think about the difference in current methods, that receive parameters, and your future ones. Think about the scope of declared variables, and how this influences sub routines, you are calling from your Main() method.
Was This Post Helpful? 0
  • +
  • -

#9 tyark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 27-September 12

Re: Help with loops VB.net

Posted 28 September 2012 - 11:52 AM

Working on it, I'll post it when I'm done :)
Was This Post Helpful? 0
  • +
  • -

#10 tyark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 27-September 12

Re: Help with loops VB.net

Posted 28 September 2012 - 09:46 PM

View Postlucky3, on 28 September 2012 - 12:30 AM, said:

Great job! Now you have wonderful opportunity to learn more from it.

Here is some information:
  • you can have (but it's not needed - so it's optional) only one method (Sub) to compare and adjust (if needed) the smallest and largest numbers;
  • none of your sub routines needs incoming parameters to compare values/display results;
  • your application breaks, if user enters non integer value.


I suggest (if you have time and will), that you try to find a solution to those 3 bits of information, and after that, compare it to your current solution. Think about what in your current code isn't needed, and why. Think about the difference in current methods, that receive parameters, and your future ones. Think about the scope of declared variables, and how this influences sub routines, you are calling from your Main() method.


So, I took your advice and condensed everything to the main sub. (almost everything)
Again, Thank you so much for helping me out! I really appreciate it!!!!

So The reason I used multiple subs is because the assignment requires it :P

And as for the fixing input if it's not an integer, I'll just wait until we learn it in class, but I am aware of the issue ;)

But here is my final code and this is actually what I will be submitting.

P.S The past code had a bug where if I entered -100, -20 and -99 It would show the largest as 0, when it should have been -20. This version fixed it!

Module Module1

    Sub Main()
        
        Dim smallest, largest, userNum As Integer
        
        showintro()
        
        smallest = -99
        largest = -99
       
        Do
            Console.Write("Please Enter a Number: ")
            userNum = CInt(Console.ReadLine())
            If userNum <= smallest Then
                smallest = userNum
            End If
            If userNum >= largest Then
                largest = userNum
            End If
        Loop Until userNum = -99
        
        showresults(largest, smallest)
    End Sub
    
    Sub showintro()
        Console.WriteLine("This program will get all of the numbers you enter ")
        Console.WriteLine("and determine the smallest and largest Number")
        Console.WriteLine("------------------------------------------------------")
    End Sub
  
    Sub showresults(ByVal largestNum As Integer, ByVal smallestNum As Integer)
        Console.WriteLine("------------------------------------------------------")
        Console.WriteLine("The largest number is: " & largestNum)
        Console.WriteLine("The smallest number is: " & smallestNum)
    End Sub
End Module

Was This Post Helpful? 0
  • +
  • -

#11 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 769
  • Joined: 19-October 11

Re: Help with loops VB.net

Posted 29 September 2012 - 12:25 AM

Well done, again. It's really good that you spotted logical error with 0 being highest number in your example. Let me add to that, that userNum variable gets value of 0 also, when it is declared. So it leads to the same logical error, but it's harder to spot. It becomes obvious, if you implement user input validation, and enter non integer value. userNum remains 0, and than it gets compared to smallest and largest value. 0 is bigger than -99, and largest gets assigned the 0 value because of it. You can see the thing in action in my code.

Let me point some things out for you, just for the sake of debate. If you move your variable declarations just outside the Main method, their scope becomes whole module (in your case they are available to the whole application). So you can call your sub routines without sending them parameters, just showresults().

In your if clauses, you don't need the =. Just for the simple demonstration, here is an example (try to run it several times, to see how it averages):
Spoiler

Edit: corrected comparison operator in example above.

As you are learning loops (based on the title of your topic), you can do (almost) the same with Do While userNum <> -99 ... Loop. In your case, Do...Loop Until is better choice, because it doesn't return to the start of the loop, just to compare what user input was.

OK, here is the code with all the things I talked about. Happy learning, and continue with your effort. You have the right mentality!

Spoiler


Edit: To clear possible confusion about TryParse logical error, here is a work-around:
Spoiler

This post has been edited by lucky3: 29 September 2012 - 04:52 AM

Was This Post Helpful? 0
  • +
  • -

#12 tyark  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 27-September 12

Re: Help with loops VB.net

Posted 29 September 2012 - 06:44 PM

Wow, thanks for the extra explanations. Also, I see what you mean about declaring the variables outside the main sub. Also thanks for showing me how to correct userNum if it is not an integer value. I also looked at your code, and there is definitely a difference when you do >= compared to just >, thanks for pointing that out.

Lastly, I looked at the code with the while loop instead of the do until loop, thanks for showing me the different ways to write the program, even though do until is better, it's nice to know that for this program a different loop works.

I cant thank you enough for sticking with me and helping me realize the errors in my code, and mentioning how to improve my code!

THANKS!!! :) :) :) :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1