Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 117,523 VB.NET Programmers for FREE! Ask your question and get quick answers from experts. There are 2,052 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



English to Metric Conversion Calculator

 
Reply to this topicStart new topic

English to Metric Conversion Calculator, I have the code written, but doesnt work under all circumstances

Boxybrown
post 2 Jul, 2008 - 09:01 PM
Post #1


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 6

Ok, so I have an assignment in my programming class and it is our first one that we need to do where we supply the entire code.
I was given a couple formulas, and told to create a conversion caluator.
The input are four numericUpDown controls, for miles, yards, feet, and inches, and when you hit compute, it will convert them to kilometers, meters and centimeters.
The example given is (entered by the user) 2 miles 5 yards 2 feet and 7 inches, which should translate to 3 kilometers, 224 meters, 5.384810769 centimeters.
With the code I've written, I can get this to work with this example, however, it doesnt seem that (nearly) any other values and I'm stumped.

We are given these formulas

Total Inches = 63360 x miles + 36 x yards + 12 x feet + inches
and
Total Meters = Total Inches / 39.37

Any clue where I went wrong?

CODE
Option Strict ON
    Const INCHESPERMILE As Integer = 63360
    Const INCHESPERYARD As Integer = 36
    Const INCHESPERFOOT As Integer = 12
    Const CONVERTER As Double = 39.37

    Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
        Dim dblMiles As Double
        Dim dblYards As Double
        Dim dblFeet As Double
        Dim dblInches As Double
        Dim dblKilometers As Double
        Dim dblMeters As Double
        Dim dblTotalInches As Double
        Dim dblRemainingMeters As Double
        Dim dblTotalMeters As Double
      
        ' Done to convert all nud values To Double
        dblMiles = Convert.ToDouble(nudMiles.Value)
        dblYards = Convert.ToDouble(nudYards.Value)
        dblFeet = Convert.ToDouble(nudFeet.Value)
        dblInches = Convert.ToDouble(nudInches.Value)

        ' Main conversion Formula
        dblTotalInches = INCHESPERMILE * dblMiles + INCHESPERYARD * dblYards + INCHESPERFOOT * dblFeet + dblInches
        dblTotalMeters = dblTotalInches / CONVERTER
        txtKilometers.Text = Convert.ToString(Convert.ToInt32(dblTotalMeters / 1000))
        dblKilometers = Convert.ToInt32(dblTotalMeters / 1000)
        dblRemainingMeters = (dblTotalMeters - (1000 * dblKilometers))
        txtMeters.Text = Convert.ToString(Convert.ToInt32(dblRemainingMeters))
        dblRemainingMeters = Convert.ToDouble(dblRemainingMeters - Convert.ToInt32(dblRemainingMeters))
        txtCentimeters.Text = Convert.ToString(dblRemainingMeters * 100)
    End Sub


This post has been edited by Boxybrown: 2 Jul, 2008 - 09:04 PM
User is offlineProfile CardPM

Go to the top of the page


Recoil
post 3 Jul, 2008 - 01:06 AM
Post #2


New D.I.C Head

*
Joined: 28 Jun, 2008
Posts: 48



Thanked 1 times
My Contributions


QUOTE(Boxybrown @ 2 Jul, 2008 - 09:01 PM) *

Ok, so I have an assignment in my programming class and it is our first one that we need to do where we supply the entire code.
I was given a couple formulas, and told to create a conversion caluator.
The input are four numericUpDown controls, for miles, yards, feet, and inches, and when you hit compute, it will convert them to kilometers, meters and centimeters.
The example given is (entered by the user) 2 miles 5 yards 2 feet and 7 inches, which should translate to 3 kilometers, 224 meters, 5.384810769 centimeters.
With the code I've written, I can get this to work with this example, however, it doesnt seem that (nearly) any other values and I'm stumped.

We are given these formulas

Total Inches = 63360 x miles + 36 x yards + 12 x feet + inches
and
Total Meters = Total Inches / 39.37

Any clue where I went wrong?

CODE
Option Strict ON
    Const INCHESPERMILE As Integer = 63360
    Const INCHESPERYARD As Integer = 36
    Const INCHESPERFOOT As Integer = 12
    Const CONVERTER As Double = 39.37

    Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
        Dim dblMiles As Double
        Dim dblYards As Double
        Dim dblFeet As Double
        Dim dblInches As Double
        Dim dblKilometers As Double
        Dim dblMeters As Double
        Dim dblTotalInches As Double
        Dim dblRemainingMeters As Double
        Dim dblTotalMeters As Double
      
        ' Done to convert all nud values To Double
        dblMiles = Convert.ToDouble(nudMiles.Value)
        dblYards = Convert.ToDouble(nudYards.Value)
        dblFeet = Convert.ToDouble(nudFeet.Value)
        dblInches = Convert.ToDouble(nudInches.Value)

        ' Main conversion Formula
        dblTotalInches = INCHESPERMILE * dblMiles + INCHESPERYARD * dblYards + INCHESPERFOOT * dblFeet + dblInches
        dblTotalMeters = dblTotalInches / CONVERTER
        txtKilometers.Text = Convert.ToString(Convert.ToInt32(dblTotalMeters / 1000))
        dblKilometers = Convert.ToInt32(dblTotalMeters / 1000)
        dblRemainingMeters = (dblTotalMeters - (1000 * dblKilometers))
        txtMeters.Text = Convert.ToString(Convert.ToInt32(dblRemainingMeters))
        dblRemainingMeters = Convert.ToDouble(dblRemainingMeters - Convert.ToInt32(dblRemainingMeters))
        txtCentimeters.Text = Convert.ToString(dblRemainingMeters * 100)
    End Sub




Forgive my noobness, but it looks like you (in second from the last line) are converting "dblRemainingMeters" and converting and subtracting "dblRemainingMeters" to Int32...I thought you had to do conversions on the same variable on separate lines...Please someone correct me blink.gif
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 3 Jul, 2008 - 04:24 AM
Post #3


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,759



Thanked 69 times

Dream Kudos: 400

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


QUOTE(Recoil @ 3 Jul, 2008 - 04:06 AM) *

Forgive my noobness, but it looks like you (in second from the last line) are converting "dblRemainingMeters" and converting and subtracting "dblRemainingMeters" to Int32...I thought you had to do conversions on the same variable on separate lines...Please someone correct me blink.gif


Correct on the Int32. All variables involved should be double, or be cast double when preforming mathmatical operations. Otherwise, the behavior is to cast the result down to what the smallest can handle.

As to separate lines, you can pile it all on! tongue.gif
User is online!Profile CardPM

Go to the top of the page

Boxybrown
post 3 Jul, 2008 - 08:52 AM
Post #4


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 6

Ok..so then what should I do?
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 3 Jul, 2008 - 08:54 AM
Post #5


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,759



Thanked 69 times

Dream Kudos: 400

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


Try this:
CODE

Const INCHESPERMILE As Double = 63360
Const INCHESPERYARD As Double = 36
Const INCHESPERFOOT As Double = 12
User is online!Profile CardPM

Go to the top of the page

Boxybrown
post 3 Jul, 2008 - 09:11 AM
Post #6


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 6

Well, I tried that, and it didnt change anything as far as the formula not creating the proper conversion. Also, I reread the instructions and it says that the first three constants have to be integers, and the last one needs to be as double. So I cant really change that. I think the problem may not be with my variables, but with the formula itself. Any ideas?
User is offlineProfile CardPM

Go to the top of the page

Recoil
post 4 Jul, 2008 - 05:38 AM
Post #7


New D.I.C Head

*
Joined: 28 Jun, 2008
Posts: 48



Thanked 1 times
My Contributions


It may help to post the entire assignment for this problem so we can get a better perspective of what exactly the problem is. On another note, if the variables absolutely have to be determined this way, you might second guess the quality of the book. I took 1 VB class at ITT and I remember where one assignment that simply could not be completed the way the book way wanting. You might ask the instructor if this problem has ever been completed before also. Just a thought.

Do you have to have the Option Strict On for this assignment?

If you do, delete the following from all of your code: Convert.ToString

This post has been edited by Recoil: 4 Jul, 2008 - 06:05 AM
User is offlineProfile CardPM

Go to the top of the page

Boxybrown
post 5 Jul, 2008 - 01:44 PM
Post #8


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 6

Well, actually, Ive been talking to a friend of mine, who is a programmer (who is in school with me also, just not the programming class) and I had him look at it, and he said the only way to make meters and centimeters read 0 instead of a negative number, is an if then statement. Either way, I'll turn it in and see what happens...I appreciate all the help tho guys
User is offlineProfile CardPM

Go to the top of the page

Recoil
post 5 Jul, 2008 - 03:43 PM
Post #9


New D.I.C Head

*
Joined: 28 Jun, 2008
Posts: 48



Thanked 1 times
My Contributions


Well, actually, with Option Strict On, VB.NET will not do implicit conversions.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/7/08 03:29PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month