3
\$\begingroup\$

Getting started in VB.net I've written this console calculator based heavily off Nicholas Dingle youtube channel (in case it looks familiar). What I want to do is have a separate sub called Operation(parameter) and take the if statements out of CollectNumbers() since it doesn't make sense to have them in that sub. To make this happen I think what i'll end up needing to do is use Enum for the different operations, but don't know how to implement it. I think also the calling of CollectNumbers() will have to be reworked as well. I'm not looking for anyone to write it out, but a nudge in the right direction or tips would be very helpful.

Imports System.Runtime.Remoting.Services
Imports System.Threading

Module Module1

Sub Main()

    Menu()

End Sub

'Enum operations

'    Add = 1
'    Subtract = 2
'    Multiply = 3
'    Divide = 4

'End Enum

Sub Menu()

    Dim selection As String

    Console.Clear()

    Console.WriteLine("Say Hello To My Little Calculator!")
    Console.WriteLine() 'print blank line
    Console.WriteLine("Please press...")
    Console.WriteLine() 'print blank line
    Console.WriteLine("(A) to Add")
    Console.WriteLine("(S) to Subtract")
    Console.WriteLine("(M) to Multiply")
    Console.WriteLine("(D) to Divide")
    Console.WriteLine("(Q) to Quit")

    selection = Console.ReadLine

    If selection.ToUpper = "Q" Then
        Console.WriteLine("Exiting...")
        Thread.Sleep(2000)
        Environment.Exit(0)
    ElseIf selection.ToUpper = "A" Then
        CollectNumbers("add")
    ElseIf selection.ToUpper() = "S" Then
        CollectNumbers("subtract")
    ElseIf selection.ToUpper() = "M" Then
        CollectNumbers("multiply")
    ElseIf selection.ToUpper() = "D" Then
        CollectNumbers("divide")
    Else
        Console.WriteLine("Please choose a valid option from the list...")
        Thread.Sleep(2000)
        Menu()
    End If

End Sub

Sub CollectNumbers(operation As String)

    Dim num1 As Integer
    Dim num2 As Integer

    Console.WriteLine("Please enter the first number...")
    num1 = CInt(Console.ReadLine)
    Console.WriteLine("Please enter the second number...")
    num2 = CInt(Console.ReadLine)

    If operation = "add" Then
        Console.WriteLine(Add(num1, num2))
    ElseIf operation = "subtract" Then
        Console.WriteLine(Subtract(num1, num2))
    ElseIf operation = "divide" Then
        Console.WriteLine(Divide(num1, num2))
    ElseIf operation = "multiply" Then
        Console.WriteLine(Multiply(num1, num2))
    End If

    Console.ReadLine()
    Menu()


End Sub

Function Add(a As Integer, b As Integer) As Double

    Return a + b

End Function

Function Subtract(a As Integer, b As Integer) As Double

    Return a - b

End Function

Function Multiply(a As Integer, b As Integer) As Double

    Return a * b

End Function

Function Divide(a As Integer, b As Integer) As Double

    Return a / b

End Function

End Module
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Thanks for the ping, I've re-read your post and glanced at the code, and you're right obviously; please accept my apologies. Consider rewording the "I think what i'll end up needing to do is use Enum for the different operations, but don't know how to implement it" part: it's what made me make the call, and likely what made others do the same. \$\endgroup\$ Commented Aug 31, 2020 at 18:32
  • \$\begingroup\$ @MathieuGuindon Understood. Still learning the etiquette on these exchange sites. \$\endgroup\$ Commented Aug 31, 2020 at 18:35
  • 1
    \$\begingroup\$ No problem! How to get the best value out of CR is without a doubt one of the best resources available to help with that. \$\endgroup\$ Commented Aug 31, 2020 at 18:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.