Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Help... Again...

Help... Again...

Scheduled Pinned Locked Moved Visual Basic
helptutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 10694570
    wrote on last edited by
    #1

    Assignment It’s often necessary to convert between units. In this exercise, you will create two functions for converting between units of distance. The first function will be called ConvertMilesToKilometers(), which will accept one parameter for the number of miles. It will return the equivalent number of kilometers. The second function will be called ConvertKilometersToMiles() and will accept kilometers as its parameter; this will return the equivalent number of miles. The main method will ask the user if he or she wants to convert miles to kilometers or kilometers to miles. It will then ask for the number of miles or kilometers. It will call the appropriate method and display the converted value. To convert miles to kilometers, divide miles by 0.62137. To convert kilometers to miles, multiply kilometers by 0.62137. Example Do you want to convert to Miles or Kilometers? (M or K): K Enter number of Kilometers: 3.5 3.5 kilometers equals 2.174795 miles.

    U 1 Reply Last reply
    0
    • U User 10694570

      Assignment It’s often necessary to convert between units. In this exercise, you will create two functions for converting between units of distance. The first function will be called ConvertMilesToKilometers(), which will accept one parameter for the number of miles. It will return the equivalent number of kilometers. The second function will be called ConvertKilometersToMiles() and will accept kilometers as its parameter; this will return the equivalent number of miles. The main method will ask the user if he or she wants to convert miles to kilometers or kilometers to miles. It will then ask for the number of miles or kilometers. It will call the appropriate method and display the converted value. To convert miles to kilometers, divide miles by 0.62137. To convert kilometers to miles, multiply kilometers by 0.62137. Example Do you want to convert to Miles or Kilometers? (M or K): K Enter number of Kilometers: 3.5 3.5 kilometers equals 2.174795 miles.

      U Offline
      U Offline
      User 10694570
      wrote on last edited by
      #2

      This is what I have so far, but I can't get past putting in the number of units to use. I feel like I'm never going to get a hang of this coding stuff. :( Module Module1 Dim miles As Decimal = 0 Dim kilometers As Decimal = 0 Dim answer As String Sub Main() Console.WriteLine("Do you want to convert miles or kilometers?") answer = Console.ReadLine() If answer = "miles" Then Console.WriteLine("What is the number of miles?") Console.ReadLine() Call convertMilesToKilometers() Console.WriteLine("The converted value is: " & kilometers) ElseIf answer = "kilometers" Then Console.WriteLine("What is the number of kilometers?") Console.ReadLine() Call convertKilometersToMiles() Console.WriteLine("The converted value is: " & miles) End If Console.ReadLine() End Sub Function convertMilesToKilometers() As Decimal kilometers = miles / 0.62137 Return kilometers End Function Function convertKilometersToMiles() As Decimal miles = kilometers * 0.62137 Return miles End Function End Module

      L T 2 Replies Last reply
      0
      • U User 10694570

        This is what I have so far, but I can't get past putting in the number of units to use. I feel like I'm never going to get a hang of this coding stuff. :( Module Module1 Dim miles As Decimal = 0 Dim kilometers As Decimal = 0 Dim answer As String Sub Main() Console.WriteLine("Do you want to convert miles or kilometers?") answer = Console.ReadLine() If answer = "miles" Then Console.WriteLine("What is the number of miles?") Console.ReadLine() Call convertMilesToKilometers() Console.WriteLine("The converted value is: " & kilometers) ElseIf answer = "kilometers" Then Console.WriteLine("What is the number of kilometers?") Console.ReadLine() Call convertKilometersToMiles() Console.WriteLine("The converted value is: " & miles) End If Console.ReadLine() End Sub Function convertMilesToKilometers() As Decimal kilometers = miles / 0.62137 Return kilometers End Function Function convertKilometersToMiles() As Decimal miles = kilometers * 0.62137 Return miles End Function End Module

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        What does "I can't get past putting in the number of units to use" mean? You are ignoring any user input except for the,

        answer = Console.ReadLine()

        line. You would have to do something just like that to get the number of miles or kilometers that the user wants to convert....

        U 1 Reply Last reply
        0
        • L Lost User

          What does "I can't get past putting in the number of units to use" mean? You are ignoring any user input except for the,

          answer = Console.ReadLine()

          line. You would have to do something just like that to get the number of miles or kilometers that the user wants to convert....

          U Offline
          U Offline
          User 10694570
          wrote on last edited by
          #4

          Oh, my goodness! I fixed the problem, and now everything works! Thank you!

          L 1 Reply Last reply
          0
          • U User 10694570

            Oh, my goodness! I fixed the problem, and now everything works! Thank you!

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Great! You're welcome!

            1 Reply Last reply
            0
            • U User 10694570

              This is what I have so far, but I can't get past putting in the number of units to use. I feel like I'm never going to get a hang of this coding stuff. :( Module Module1 Dim miles As Decimal = 0 Dim kilometers As Decimal = 0 Dim answer As String Sub Main() Console.WriteLine("Do you want to convert miles or kilometers?") answer = Console.ReadLine() If answer = "miles" Then Console.WriteLine("What is the number of miles?") Console.ReadLine() Call convertMilesToKilometers() Console.WriteLine("The converted value is: " & kilometers) ElseIf answer = "kilometers" Then Console.WriteLine("What is the number of kilometers?") Console.ReadLine() Call convertKilometersToMiles() Console.WriteLine("The converted value is: " & miles) End If Console.ReadLine() End Sub Function convertMilesToKilometers() As Decimal kilometers = miles / 0.62137 Return kilometers End Function Function convertKilometersToMiles() As Decimal miles = kilometers * 0.62137 Return miles End Function End Module

              T Offline
              T Offline
              Tino Fourie
              wrote on last edited by
              #6

              Not entirely sure what Language you are using here but it looks very much like VB. Therefore assuming that it is VB. On the surface your code looks correct. If I had to rewrite the code it would look something like this:

              Module Module1

              Dim distance As Decimal = 0
              Dim answer As String = nothing

              Sub Main()
              Console.WriteLine("Do you want to convert miles or kilometers? (M or K)")
              answer = Console.ReadLine()

              Console.WriteLine("What is the distance?")
              distance = Console.ReadLine()

              If answer = "M" Then
              Console.WriteLine("The converted value is: " convertToKM(distance))
              Else
              Console.WriteLine("The converted value is: " & converToML(distance))
              End If

              Console.ReadLine()
              End Sub

              Function convertToKM(distance) As Decimal
              Dim km as Decimal = 0

              km = distance / 0.62137
              Return km
              End Function

              Function convertToML(distance) As Decimal
              Dim ml as Decimal = 0

              ml = distance * 0.62137
              Return ml
              End Function

              End Module

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups