Code that converts Usd to cad using visual basic application console

Zaza Aus 1 Reputation point
2021-11-09T23:35:19.977+00:00

Hi, i could use some help with a code visual basic application console that converts USD into CAD.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,778 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Devon Nullman 21 Reputation points
    2021-11-10T02:49:52.557+00:00

    What have you done so far ?
    Example, no error checking:

    Imports System
    
    Module Program
        Sub Main(args As String())
            Dim USD As Decimal
            Dim Rate As Decimal
            Console.Write("How many US Dollars ? ")
            USD = CDec(Console.ReadLine)
            Console.WriteLine()
            Console.Write("What is the Conversion Rate ? ")
            Rate = CDec(Console.ReadLine)
            Console.WriteLine()
            Console.WriteLine(USD.ToString & " US Dollats = " & (Rate * USD).ToString & " Canadian Dollars")
            Console.WriteLine("ENTER to exit")
            Console.ReadLine()
        End Sub
    End Module
    
    0 comments No comments

  2. Jiachen Li-MSFT 33,446 Reputation points Microsoft Vendor
    2021-11-10T06:54:19.797+00:00

    Hi @Zaza Aus ,
    The following code implements exchange rate conversion, you can refer to it.
    The exchange rate is set to a recent average of 1.24.

            Dim USD As Single  
            Dim ExchangeRate As Single = 1.24  
            While True  
                Try  
                    Console.WriteLine("Enter the USD:")  
                    USD = CSng(Console.ReadLine())  
                    Console.WriteLine(USD & " USD = " & USD * ExchangeRate & " CAD")  
                Catch ex As Exception  
                    Console.WriteLine("Please enter the correct value")  
                End Try  
            End While  
    

    Hope the code above could be helpful.
    Best Regards.
    Jiachen Li
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Castorix31 86,966 Reputation points
    2021-11-10T07:16:34.303+00:00

    The exchange rate is not fixed, then you must read it from the Net

    Like :

    Dim request As WebRequest = WebRequest.Create("http://www.floatrates.com/daily/cad.json")
    request.Credentials = CredentialCache.DefaultCredentials
    Dim response As WebResponse = request.GetResponse()
    Dim dataStream As Stream = response.GetResponseStream()
    Dim reader As StreamReader = New StreamReader(dataStream)
    Dim responseFromServer As String = reader.ReadToEnd()
    Dim sRateBegin As String = """rate"":"
    Dim sRateEnd As String = ","
    Dim sRate As String = responseFromServer.Substring(responseFromServer.IndexOf(sRateBegin) + sRateBegin.Length)
    sRate = sRate.Substring(0, sRate.IndexOf(sRateEnd))
    Dim nRate As Double = 0.0
    Double.TryParse(sRate, System.Globalization.NumberStyles.Number, System.Globalization.CultureInfo.InvariantCulture, nRate)
    Console.WriteLine("1 USD = {0} CAD", (1 / nRate).ToString())
    reader.Close()
    response.Close()
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.