Smaller Number

TommyEvans

Active member
Joined
Feb 19, 2010
Messages
39
Programming Experience
Beginner
Alright. I'm making a program for a class, as usual. I need it to grab the smallest number, of the 2 inputted numbers.
Well, there is 4 inputted numbers. For instance the user inputs the following:
Price Ounces

20 4
12 4

I divide the numbers, and get 5 and 3.

The better buy is going to be item 2, as it is $3 per ounce. I can explain more if I need to.

But I need the program to grab the smallest number AFTER dividing, and then display that in a label. I need to know how to grab the smallest number. What statement do I use?

Odds are, it's something simple and I'm overlooking it. I'm quite tired, I'm having a nicotine fit, and I can't concentrate from all of the noise.
 
Last edited:
A simple if statement should do.

VB.NET:
    Private Sub Calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calculate.Click
        Dim number1 As Integer = CInt(num1.Text)
        Dim number2 As Integer = CInt(num2.Text)
        Dim number3 As Integer = CInt(num3.Text)
        Dim number4 As Integer = CInt(num4.Text)
        resultLabel.Text = If((number1 / number2) > (number3 / number4), "Second Pair is Smaller", "First Pair is Smaller")
    End Sub
 
You need separate variables for each computation. Example:
ResultA = 20 / 4
ResultB = 12 / 4

Then use an If block to decide which result is smaller.
 
veqhk5.jpg


This might help. I think I understand how to show the smaller number now. Now, how would I go about displaying the unit price per ounce, of the better buy? :S

Edit: I got it. Now, I have a problem. It's giving me an error. :S

Here is the code:
VB.NET:
Public Class grocery_calc
    Dim Price1 As Decimal = CInt(txtPrice1.Text)
    Dim Price2 As Decimal = CInt(txtPrice2.Text)
    Dim Ounce1 As Decimal = CInt(txtOunces1.Text)
    Dim Ounce2 As Decimal = CInt(txtOunces2.Text)


    Private Sub GoToNCVPSToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoToNCVPSToolStripMenuItem.Click
        Process.Start("http://www.ncvps.com/")

    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtPrice1.Text = ""
        txtOunces1.Text = ""
        txtPrice2.Text = ""
        txtOunces2.Text = ""
        lblBetter.Text = ""
        lblCost.Text = ""
    End Sub

    Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
        Application.Exit()

    End Sub

    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        If (Price1 / Ounce1) > (Price2 / Ounce2) Then
            lblBetter.Text = "Item #1 is the better buy!"
            lblCost.Text = "The Cost Per Ounce is $" & (Price1 / Ounce1)

        Else
            lblBetter.Text = "Item #2 is the better buy!"
            lblCost.Text = "The Cost Per Ounce is $" & (Price2 / Ounce2)
        End If
    End Sub
End Class

It's giving me the following error when I try to debug:

skx0cx.jpg
 
Last edited:
the offending code is:

VB.NET:
    Dim Price1 As Decimal = CInt(txtPrice1.Text)
    Dim Price2 As Decimal = CInt(txtPrice2.Text)
    Dim Ounce1 As Decimal = CInt(txtOunces1.Text)
    Dim Ounce2 As Decimal = CInt(txtOunces2.Text)

declaring them is fine, but telling your program to assign Cint(value) without a value will product an error.
Instead, assign the values when you press submit.

VB.NET:
    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Price1 = CDec(txtPrice1.Text)
        Price2 = CDec(txtPrice2.Text)
        Ounce1 = CDec(txtOunces1.Text)
        Ounce2 = CDec(txtOunces2.Text)

        If (Price1 / Ounce1) > (Price2 / Ounce2) Then
            lblBetter.Text = "Item #1 is the better buy!"
            lblCost.Text = "The Cost Per Ounce is $" & (Price1 / Ounce1)
        Else
            lblBetter.Text = "Item #2 is the better buy!"
            lblCost.Text = "The Cost Per Ounce is $" & (Price2 / Ounce2)
        End If
    End Sub

and if you declare as decimal, then use CDec, not Cint.
 
Alright. Sorry to bug everyone, but one last thing.

When I divide, and it comes out to a decimal digit of, for example: $4.5232323
How would I just show, $4.52. Or the last 2 digits of the decimal only? Or for instance, round it?
 
More precisely (and some explanations for this behaviour), if you look at the stacktrace it will point out the problem, which is explained by the InnerException that is a NullReferenceException pointing to line number of "Dim Price1 As Decimal = CInt(txtPrice1.Text)". You could say that the outer problem was that 'some kind of error occurred that prevented the form from being created', and the inner problem is specifically that line of code.

These variables are initialized when the grocery_calc class instance is created, this happens before any controls in that form are created, so the txtPrice1 variable currently points to Nothing. If you would add a Sub New (constructor) to a form you will see a generated call to InitializeComponent method, this is where the Designer generated code for all the controls is carried out, all the things you do in Designer causes lines of code to be added to this method.

I see no reason for you to declare these variable at class level, you should declare them locally when you need to use them, that is in the handler for Submit button. (same as MattP and r3plica suggested)
 
you could use Ounce2.ToString("0.00")
 
Right here:
VB.NET:
        If (Price1 / Ounce1) > (Price2 / Ounce2) Then
            lblBetter.Text = "Item #1 is the better buy!"
            lblCost.Text = "The Cost Per Ounce is $" & (Price1 / Ounce1)
        Else
            lblBetter.Text = "Item #2 is the better buy!"
            [B]lblCost.Text = String.Format("The Cost Per Ounce is ${0}", (Price2 / Ounce2).ToString("0.00"))[/B]
        End If
 
Back
Top