Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic (http://www.programmingforums.org/forum18.html)
-   -   Visual Basic Converter Problem (http://www.programmingforums.org/showthread.php?t=11498)

dwizzle13 Oct 4th, 2006 7:48 PM

Visual Basic Converter Problem
 
Hi, I'm trying to make a program to convert us to metric, where miles go to kilometers, yards to meters, and feet to centimeters. I just can't understand the y to m, and f to cm, since i need to do it in a specific way for my class. Here's the code w/ explanations at end.


:

Dim Miles, Yards, Feet, Inches, totalInches, totalMeters As Double
        Dim km, m, cm As Double
        'assign values from txt boxes
        Miles = txtMiles.Text
        Yards = txtYards.Text
        Feet = txtFeet.Text
        Inches = txtInches.Text

        'convert to inches
        totalInches = (63360 * Miles) + (36 * Yards) + (12 * Feet) + Inches
        Miles = 63360 * Miles
        Yards = 36 * Yards
        Feet = 12 * Feet

        'convert to meters
        totalMeters = totalInches / 39.37
        Miles = Miles / 39.37
        Yards = Yards / 39.37
        Feet = Feet / 39.37

        km = Miles
        m = Yards
        cm = Feet



        km = Int(Miles / 1000)
        m = Int((totalMeters - Miles) / 1000)
        cm = Math.Round(totalMeters - (km + m), 1)

        'display
        cm = Math.Round(cm, 2)
        With lstOutput.Items
            .Add("The metric length is")
            .Add(km & " kilometers")
            .Add(m & " meters")
            .Add(cm & " centimeters")
            .Add(totalInches)
            .Add(totalMeters)
        End With




I was trying to convert, w/ these formulas, and tested it w/ these input numbers:
formula1, totalInches = 63360 * miles + 36 * yards + 12 * feet + inches
#2 totalMeters = totalInches / 39.37
Note: I have no idea why i need the total inches or meters.
kilometers = Int(meters/1000), i use miles instead of meters and it works.
input #'s
Miles = 5
Yards = 20 to 8 only this one works for me
Feet = 2 to 65
Inches = 4 to 73.5

please help, thanks, i just need even at least tips on what to do, even just explanations, thanks.

DaWei Oct 4th, 2006 7:51 PM

As a new member, you should read the forum's Rules/FAQ, specifically, in this case, in regard to code tags. Anything less is rude to those from whom you seek help.

dwizzle13 Oct 4th, 2006 7:54 PM

sorry, didn't notice tht, thanks. BTW, the error was that i got riddiculios numbers on the meters and centimeters when i displayed them.

DaWei Oct 4th, 2006 8:06 PM

Your code is illogical and your naming conventions are confusing. Miles is not equal to 63360 * Miles. That gives you inchesPerMile. Still, a name is just a name if you don't get confused. The problem is that you are multiplying Miles by 63360 at the beginning of your code, and again in line 4. That gives you 63360*63360*Miles, which is a somewhat larger number than you want. Similarly, elsewhere.

dwizzle13 Oct 4th, 2006 8:12 PM

i fixed the miles, but the miles = 63360 * Miles the first time is because i had to change 5 miles into its inch equivilent, and the same w/ the others, but i don't know why, it was just the way the problem goes. So what else is wrong? I'll update the above code.

Booooze Oct 4th, 2006 8:15 PM

I don't really get what your doing with your code. It should be simple. Perhaps your over thinking it.
:

Feet = CM * 30.48
Metres = Yards * 0.9144


How hard is that to throw into the equation?

I'm not amzing at math, and never realyl had to bother with conversions, but if you get your math right, consider posting all of your code. Also, as you said, you have to do it a paticular way for your class, so it may not be as easy as just that. The class is to teach you programming, not math. If you have problems with the math, simply google the equations. the google calculator always helps me. I have noticed that you haven't declared any of the variables. Some might tell you that vb is sloppy and will do it for you. In some cases that is true, but I have experiences that say otherwise.

dwizzle13 Oct 4th, 2006 8:18 PM

ok, i don't think it'll work, i'm trying to convert us to metric. I'll post the full code above.

DaWei Oct 4th, 2006 8:19 PM

You can't multiply them twice. The yards and feet things have the same problem. You are going to have to choose one or the other. You can say,
:

Miles = 63360 * Miles
Yards = 36 * Yards
Feet = 12 * Feet
totalInches = Miles + Yards + Feet

or, you can say,
:

totalInches = (Miles * 63360) + (Yards * 36) + (Feet * 12)
You can't do both. If you correct that and have additional problems, post back with the fresh (corrected) code. It would also help if you post enough for us to see the source of the values you are manipulating and explain precisely what you are supposed arrive at with those values. Information is key here; my crystal is in the shop for 'ball' joints.

dwizzle13 Oct 4th, 2006 8:25 PM

Ok, here's the fresh code, my only problem is the answers(output) of meters and cm are 0 meters, and 8057.6 cm. I needed to get an answer of 65 meters and 73.5 centimeters.



:

Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
        Dim Miles, Yards, Feet, Inches, totalInches, totalMeters As Double
        Dim km, m, cm As Double
        'assign values from txt boxes
        Miles = txtMiles.Text
        Yards = txtYards.Text
        Feet = txtFeet.Text
        Inches = txtInches.Text

        'convert to inches
        Miles = 63360 * Miles
        Yards = 36 * Yards
        Feet = 12 * Feet
        totalInches = Miles + Yards + Feet

        'convert to meters
        totalMeters = totalInches / 39.37
        Miles = Miles / 39.37
        Yards = Yards / 39.37
        Feet = Feet / 39.37

        km = Miles
        m = Yards
        cm = Feet



        km = Int(Miles / 1000)
        m = Int((totalMeters - Miles) / 1000)
        cm = Math.Round(totalMeters - (km + m), 1)

        'display
        cm = Math.Round(cm, 2)
        With lstOutput.Items
            .Add("The metric length is")
            .Add(km & " kilometers")
            .Add(m & " meters")
            .Add(cm & " centimeters")
            .Add(totalInches)
            .Add(totalMeters)
        End With

    End Sub


dwizzle13 Oct 4th, 2006 8:36 PM

srry about missing tags, etc... it's hard to do this prog, and i need help since i have no idea about it, especially this part:

:

km = Int(Miles / 1000)
        m = Int((totalMeters - Miles) / 100)
        cm = Math.Round(totalMeters - (km + m), 1)



All times are GMT -5. The time now is 11:24 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC