# Program to find the smallest number from inputboxes

• (2 Pages)
• 1
• 2

## 16 Replies - 2774 Views - Last Post: 20 June 2012 - 01:35 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=256873&amp;s=43ed4163cb99d3c58283f4a42b90b975&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 BigdWiki

Reputation: -4
• Posts: 14
• Joined: 22-November 11

# Program to find the smallest number from inputboxes

Posted 22 November 2011 - 10:38 PM

Hey guys. I've got a homework problem that has me stumped. We haven't gotten to arrays yet, so I can't use them to do the problem. Here it is: "Write a program that finds the smallest number in a sequence of non negative numbers entered by the user from dialog boxes. The user should be told to type in the number -1 to indicate that the entire sequence has been entered." I'm supposed to be doing this with a loop.

I was thinking of doing a listbox with all of the entries, then sorting the listbox smallest to largest, then taking the top number and using it. Trouble is, I'm stumped on how to make it work. Ideally I'd like it to be done with a loop instead of the listbox.

Here's what I've got so far.
```Dim num As Double
Dim count As Double
Dim sum As Double
Dim prompt As String = "Enter a nonnegative number. Enter -1 to terminate."
Dim list As String = ListBox1.Text

num = CDbl(InputBox(prompt))
Do While num <> -1
ListBox1.Text = CStr(num)
Loop

```

Any ideas on how to do this? Thanks!!

Is This A Good Question/Topic? 0

## Replies To: Program to find the smallest number from inputboxes

### #2 DimitriV

• Don't try to save yourself… the circle is complete

Reputation: 544
• Posts: 2,632
• Joined: 24-July 11

## Re: Program to find the smallest number from inputboxes

Posted 22 November 2011 - 11:01 PM

Well, first you need to add them to the ListBox. I normally use VB.Net but I don't think you add items by setting the Text property. Then worry about sorting the items.

### #3 BigdWiki

Reputation: -4
• Posts: 14
• Joined: 22-November 11

## Re: Program to find the smallest number from inputboxes

Posted 22 November 2011 - 11:03 PM

True, its ListBox1.items.add. Any way to do this using a loop? The listbox will work as a last resort though.

### #4 maj3091

• D.I.C Lover

Reputation: 273
• Posts: 1,635
• Joined: 26-March 09

## Re: Program to find the smallest number from inputboxes

Posted 23 November 2011 - 01:00 AM

If you're only interested in the smallest number and don't need to display the other entries or show them sorted, then try using a variable to hold the smallest value entered, then each time a new entry is made, compare it to this variable, if it's smaller, replace it, if not continue.

Storing the numbers and sorting them is more work than required, assuming you don't need to.

### #5 BigdWiki

Reputation: -4
• Posts: 14
• Joined: 22-November 11

## Re: Program to find the smallest number from inputboxes

Posted 23 November 2011 - 07:59 AM

Could you give me an example of how this is done? I'm not sure how to store the entries as variables.

### #6 maj3091

• D.I.C Lover

Reputation: 273
• Posts: 1,635
• Joined: 26-March 09

## Re: Program to find the smallest number from inputboxes

Posted 23 November 2011 - 08:27 AM

This code assigns a value to a variable
```num = CDbl(InputBox(prompt))

```

```Dim EnteredValue as DataType
' Get the value from the user
EnteredValue = InputValue

'Check if the entered value is less than the stored value
If  EnteredValue < StoredValue then
' yes it is, so make the entered value the new stored value
StoredValue  = EnteredValue
endif

```

That should give you a starting point, so now try filling in some of the blanks.

### #7 BigdWiki

Reputation: -4
• Posts: 14
• Joined: 22-November 11

## Re: Program to find the smallest number from inputboxes

Posted 23 November 2011 - 08:32 AM

Thanks! How do I get the StoredValue though? It isn't comparing against a set value, its trying to keep popping up an inputbox in a loop, and finding the lowest one. There isn't a set number of times the inputbox pops up either, it keeps going until the user enters -1.

### #8 maj3091

• D.I.C Lover

Reputation: 273
• Posts: 1,635
• Joined: 26-March 09

## Re: Program to find the smallest number from inputboxes

Posted 23 November 2011 - 09:12 AM

Well, you started doing your loop in the original code post, so that if the user enters -1 it's finished, so try using something like that.

In regards to the StoredValue, initially, you won't have one, so the first value you enter will become the stored value.

You've got all the information you need to do this now, so have a go at it yourself and post back your code and any issues you have.

### #9 guyfromri

Reputation: 43
• Posts: 777
• Joined: 16-September 09

## Re: Program to find the smallest number from inputboxes

Posted 23 November 2011 - 09:37 AM

My own 2 cents...the problem itself sounds mildly tedious but it will get worse if the variables keep throwing stupid errors --

Dim Count As Double

I would make this look more like

Dim dblCnt as Double

I believe Count is already built in to vb for some purpose or another --

I'd also do some googling on Sum -- This one I've never used but it seems to me that VB would already have this used somewhere -- I've had these headaches in the past so I always watch for things like that --

Hope it helps, somewhat

### #10 BobRodes

Reputation: 550
• Posts: 2,911
• Joined: 19-May 09

## Re: Program to find the smallest number from inputboxes

Posted 30 November 2011 - 12:20 PM

I wouldn't use a double here either. An integer is more economical, and you don't need decimal places.

### #11 Guesty

Reputation: 0
• Posts: 1
• Joined: 17-June 12

## Re: Program to find the smallest number from inputboxes

Posted 17 June 2012 - 09:43 PM

Yeah i've also come up against this question and as its one that tends to pop up every year.
So here's the one way to do it in VB net:

```Public Class Form1

Private Sub btnDisplay_Click(sender As System.Object, e As System.EventArgs) Handles btnDisplay.Click

Dim Num As Double = 0
Dim storedvalue As Integer
Dim Prompt As String = "Enter a positive number. " &
"Enter -1 to terminate entering numbers."

Num = CDbl(InputBox(Prompt))
storedValue = Num                            'Sets Stored value

Do While Num <> -1

If Num < storedvalue Then
Num = storedvalue                     'Compares number num to original stored value
Else
storedvalue = storedvalue
End If
Num = CDbl(InputBox(Prompt))
Loop

txtDisplay.Text = storedvalue

End Sub
End Class

```

All thats needed is a textbox and button. And no need for sorting numbers in a listbox.
All it does is assigns the first value to be input to the stored value. Then it is compared to other values being imput. And smallest number will always equall the stored value.

Hopes this helps anyone.

### #12 Neku

• D.I.C Regular

Reputation: 18
• Posts: 255
• Joined: 21-May 09

## Re: Program to find the smallest number from inputboxes

Posted 18 June 2012 - 10:20 AM

first of all make all your variables as integer, its cheaper then double (unless you do expect the user to input numbers with decimal)

to find out if the user input -1 put this code at the top of the subrotine right after you get the input into a variable.

This will also display what was the last smallest number a user input
```If EnteredNum= -1 then
Msgbox "The smallest number is: " & SmallestNum
Exit Sub
End if

```

this as i said will end the loop that get numbers when user input -1 and will pop a message with the last smallest number that was input.

you could use a Do While loop after this code in order to get numbers and check if they are smaller or you could use the GoTo statement and make it go each time to the beggining of the sub, in essence a never ending loop until user input -1.

(for others please dont kill me for offering the GoTo statement )

### #13 BobRodes

Reputation: 550
• Posts: 2,911
• Joined: 19-May 09

## Re: Program to find the smallest number from inputboxes

Posted 19 June 2012 - 09:05 AM

<for others please dont kill me for offering the GoTo statement

Neku
Neku
<Neku
<Neku
<Neku

Ok, I won't. But I sure want to.

This post has been edited by BobRodes: 19 June 2012 - 09:08 AM

### #14 Neku

• D.I.C Regular

Reputation: 18
• Posts: 255
• Joined: 21-May 09

## Re: Program to find the smallest number from inputboxes

Posted 19 June 2012 - 09:50 AM

*sniff* why? i was a good student, you teached me this T__T"

really how horrible this idea is? i think it can save few cycles from the CPU (even if preformance is not the problem here) and it can ease the headache from debugging Do While loops (i usually only use these annoying loops when reading from a file { and usually use the GoTo when handling errors while using files })

i know GoTo is not a good practice but here its fresh and harmless

### #15 BobRodes

Reputation: 550
• Posts: 2,911
• Joined: 19-May 09

## Re: Program to find the smallest number from inputboxes

Posted 19 June 2012 - 11:24 AM

LOL it's true, I sometimes use it too, to avoid convoluted if blocks and/or subroutine calls when certain conditions require going to the top of the loop without executing most of the code. Comes from my old Paradox-for-DOS days, where the "Loop" command means go to the top of the loop, rather than being the equivalent of "Wend." I used that a lot, and didn't much like doing without it in my early VB4 days.

I guess you're getting old when something always seems to remind you of something else.

This post has been edited by BobRodes: 19 June 2012 - 11:25 AM