Visual Basic Mod Functions...

5LiterMustang

Senior member
Dec 8, 2002
531
0
0
Guys I"m working on some VB homework and I've gotta use a mod function...I'll breifly describe the problem, I've got to convert miles, yards, feet and inches to kilometers, meters and centimeters...should be fairly straight forward, I just lost my notes from class on the VB mod fuction and need some help structuring it...

 

5LiterMustang

Senior member
Dec 8, 2002
531
0
0
Cool thanks, I think I did that right...but now when I actually try to run the program it gives me an overflow error...any clue? I can cut and past the code if you'd like
 

5LiterMustang

Senior member
Dec 8, 2002
531
0
0
Option Explicit
Private Sub cmdConvert_Click()

Dim intMiles As Integer, intYards As Integer, intFeet As Integer, intInches As Integer
Dim intKilo As Integer, intMeter As Integer, sngCenti As Single
Dim lngTotInch As Long, intTotMeters As Integer, intTotKilo As Integer
Dim x As Integer

intMiles = Val(txtMiles)
intYards = Val(txtYards)
intFeet = Val(txtFeet)
intInches = Val(txtInches)

lngTotInch = ((63360 * intMiles) + (36 * intYards) + (12 * intFeet) + intInches)
intKilo = lngTotInch \ 39370
x = lngTotInch Mod 39370
intMeter = x \ 39.37
x = intMeter Mod 39.37
sngCenti = x / 2.54


picOutput.Print "Number of Kilometers = "; intKilo
picOutput.Print "Number of Meters = "; intMeter
picOutput.Print "Number of Centimeters = "; sngCenti



End Sub

I figured out the overflow problem its because I needed to convert one of my variables to long...so I did that...its working most of the way now, except I've got some sort of problem in the mod functions be cause its not converting the centimters properly...for example if I say 1 mile, 1 yard, 1 foot and 1 inch...or 1 mile 1 yard 1 foot and 3 inches...the numbers stay the same...the centimeter number should have gone up by about 5 and it did not...
 

Saynac

Member
Apr 12, 2000
68
0
0
Mustang, your first problem is that you have X as an integer. Since X gets the value of "lngTotInch MOD 39370", that means that X can be in the range of 0 up to +39,369. An integer can only hold up to +32,767. Change that sucker to a single.

Your second problem lies in the MOD function and how you are using it. This example is from the VB help file on the MOD operator: If you call "19 Mod 6.7", what does it return? Your program is built around the assumption that it would return 5.6 but it does not. The answer is truncated to exactly 5. So, when you mod intMeter by 39.37, you are getting a truncated return value. That would be part of the problem.

Solve both of these problems and you should be home free. The rest of your code looks good to me.


Edit: I explained the MOD function incorrectly, it seems. When you call "numA MOD numB", both numA and numB are converted to integers before the mod operation is applied. So, in the above example of "19 MOD 6.7", what is really happening is the function is interpreting it as "19 MOD 7". So, when you call "intMeter MOD 39.37", you are getting "intMeter MOD 39". Cool?