ask the user to input?

AtWitsEnd

New member
Joined
Nov 20, 2009
Messages
1
Programming Experience
Beginner
For my visual Basic class i have to write a console application in VB 2005.

The program is supposed to repeatedly ask the user to input a random number until the user enters -1 at which point the program will display the Highest Number and Lowest Number entered by the user.

I've been working on this for three days but to no avail. it just won't work.

could someone please help or give some sagely advice? thank you.
 
Some hints:

Use a variable for high and another for low.
Get the user's first input and save the value to both high and low.
Start your Do loop.
Place the next user input inside the loop.
Compare the input with high and with low.
If the input is higher than high, or lower than low, save the new value.
Stop the loop when the input is -1 without saving it.


Post your code if you are still having a problem.
 
I agree with Hack you outta post what you have especially since it's for class I wouldn't want to give you the answer. I know how to do t and I am quite sure that everyone else on these forums would be albe to help. It would be best if we point out where you went wrong so you can learn :)
 
Yeah, I agree with both hack and Biohazzard. Its not a hard task to accomplish, but when you work that closely with the project, sometimes you wont see the easiest answer. Post what you got up here, and we can point yah in the right direction.

The main advantage to this is so that you can get pointers on how to get your code to work, without having to rewrite you project to match someone elses. Plus, if you doing something the hard way, someone could explain the easier way, and why.
 
Well you haven't replied so perhaps we should assume that you got it?

But in case you didn't here's what I would do:




Dim high, low As Integer
Dim a As Integer

Console.WriteLine("Please enter a number.")
a = Console.ReadLine()
If a = -1 Then
Console.WriteLine("You only entered -1.")
GoTo terminate
Else : high = a
low = a
End If

Console.WriteLine()
Console.WriteLine("Please enter a number.")
a = Console.ReadLine()
If a = -1 Then
If a > high Then
Console.WriteLine("The highest number you entered was " & a)
Console.WriteLine("The lowest number you entered was " & high)
GoTo terminate
Else : Console.WriteLine("The highest number you entered was " & high)
Console.WriteLine("The lowest number you entered was " & a)
GoTo terminate
End If
ElseIf a > high Then
high = a
ElseIf a < low Then
low = a
End If


Do
Console.WriteLine()
Console.WriteLine("Please enter a number.")
a = Console.ReadLine()
If a = -1 Then
Console.WriteLine("The highest number you entered was " & high)
Console.WriteLine("The lowest number you entered was " & low)
ElseIf a > high Then
high = a
ElseIf a < low Then
low = a
End If
Loop Until a = -1

terminate:
Console.WriteLine()
Console.WriteLine()
Console.WriteLine("The program will now terminate.")
Console.ReadLine()
End




Of course, there are probably better ways; there are definitely other ways; and there are almost certainly shorter ways.

Out of interest, and I would welcome criticism (provided it is not just slating me), what would others change about my code?
I feel that it could be considerably shorter, but I think the first two "chunks" are necessary, in case the first or second number input = -1.
 
username said:
what would others change about my code?
The first thing that goes is the use of Goto, a big no-no in structured programming. Since this "homework" is long overdue I will also provide a possible solution.

About the exercise:
ask the user to input a random number until the user enters -1 at which point the program will display the Highest Number and Lowest Number entered by the user
Before attempting any implementation, one have to understand the problem, or at least tried to and decided how to handle it. The most important part here is "number entered by user", meaning user could possibly not have entered any valid numbers, and the input must validate as a number. There is also no mention of negative/positive numbers, or if they should be whole numbers. I also read that -1 should be excluded from the high/low calculation, that the only function for this particular number is to be a stop signal for the loop. If -1 should be considered also a valid input for the high/low calculation before the loop ends there must be some other way to determine if user has entered a number or not (since a Integer variable always have default number 0), for example a Boolean or using a Nullable Integer for the high/low variables. The exercise says nothing about initial values, so the first valid number entered must be considered both the highest and the lowest number entered. I think that sums up the considerations one need to evaluate. For this implementation I choose whole numbers only and allow negative numbers for values in Integer type range. This code give no feedback about invalid input, but it could easily be included, it does output if user has not entered any numbers.
VB.NET:
Dim tmp, low As Integer, high As Integer = -1

Do
    Console.Write("Enter a number (-1 to stop): ")
    Dim input As String = Console.ReadLine()
    If Integer.TryParse(input, tmp) Then
        If tmp = -1 Then Exit Do 'stop signal
        If high = -1 Then 'first valid number entered
            high = tmp
            low = tmp
        Else 'high/low calculation
            high = Math.Max(high, tmp)
            low = Math.Min(low, tmp)
        End If
    End If
Loop

If high <> -1 Then
    Console.WriteLine("The highest number entered was: " & high.ToString)
    Console.WriteLine("The lowest number entered was: " & low.ToString)
Else
    Console.WriteLine("No numbers was entered.")
End If
 
Back
Top