help converting ounces to remaining gallons, quarts, pints, cups and ounces

flashy_lord

Member
Joined
Sep 3, 2010
Messages
5
Location
wisconsin
Programming Experience
Beginner
hey gals

this whole concept with MOD and integer division has me tangled up, I have to write some code that will convert ounces to remaining gallons, quarts, pints, cups and ounces using integer division and mod operator

this are the converting factors
one gallon = 128 ounces
one quart = 32 ounces
one pint = 16 ounces
one cup = 8 ounces

for some reason my logic is not flowing correctly, can anyone help me straighten this out? my code is as follows

Public Class LiquidCalculator


Private Sub convertButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles convertButton.Click
Dim Gallons, Quarts, Pints, Cups, Ounce, Tbox As Integer

Tbox = Val(OunceTextBox.Text)
Gallons = (Tbox \ 128)
GlLabel.Text = Gallons

Quarts = (Gallons \ 32) Mod 32
QtLabel.Text = Quarts

Pints = (Quarts \ 16) Mod 16
PtLabel.Text = Pints

Cups = (Pints \ 8) Mod 8
CpLabel.Text = Cups

Ounce = (Cups \ 4) Mod 4
End Sub
End Class


I have also attached the solution to this thread in case that helps making more sense
View attachment liquid converter.zip
 
MOD operator gives you the remainder of integer division, for example you have value 135; integer division 135\128=1 (gallons), remainder 135 MOD 128 = 7 (to be divided in other units).
So each time you calculate a unit you also need to calculate the remainder, for next unit you use the remainder as starting value.
 
thanks a lot, your suggestion was very helpful and and it gave me a push start into making the logic of my application flow correctly, Im done with the application now, I ran it a few times and it has no bugs, again thanks a lot John
 
Back
Top