PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VB calculator functions-VBForums
Results 1 to 9 of 9

Thread: VB calculator functions

  1. #1

    Thread Starter
    Registered User
    Join Date
    Feb 2018
    Posts
    2

    VB calculator functions

    Hi I'm new to programming and just started learning.
    My second week assessment requires me to use a console app to basically create a calculator.
    Problem is I have been trying for hours to get it to work, at this point it's not meant to be a working calculator just some basic code and a few lines of question etc. here is the question as well as my code thus far
    "Use a Console Application for the following exercises"
    "Write a program that asks the user for 2 numbers then asks the user whether to add, subtract, multiply or divide them. Display the result"
    Code:
    Sub Main()
            '******* Cert IV Programming 14-2-18
            Dim n0, n1, a0 As Integer 'number 0 and number 1 and answer
            Dim f, add, subtract, multiply, divide As String 'function
            Console.WriteLine("This programme will check which number is larger")
            n0 = InputBox("please enter the first number")
            Console.WriteLine("You have entered " & n0)
            n1 = InputBox("please enter the second number")
            Console.WriteLine("You have entered " & n1)
            f = InputBox("Would you like to add, subtract, divide or multiply?")
            Console.WriteLine("" & f)
            Console.ReadLine()
    
            If f = add Then
                a0 = (n0 + n1)
                Console.WriteLine(" " & a0)
            ElseIf f = subtract Then
                a0 = (n0 - n1)
                Console.WriteLine(" " & a0)
            ElseIf f = multiply Then
                a0 = (n0 * n1)
                Console.WriteLine(" " & a0)
            ElseIf f = divide Then
                a0 = (n0 / n1)
                            Console.WriteLine(" " & a0)
                        End If
                        Console.ReadLine()
        End Sub
    Everything displays except the result (a0)
    I'm feeling rather stupid, the last time I programmed anything was 20 years ago in Qbasic
    I don't expect anyone to write/do my assessment just please tell me what I'm doing wrong.
    Last edited by dday9; Feb 14th, 2018 at 09:24 AM.

  2. #2
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    372

    Re: VB calculator functions

    The question I would pose to you is this:

    You define f and add as String variables. When your code reaches the line of code below, what is the value of f, and what is the value of add?

    Code:
    If f = add Then

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,420

    Re: VB calculator functions

    Please use appropriate formatting tags when posting code snippets, for readability.
    vb.net Code:
    1. Sub Main()
    2.         '******* Cert IV Programming 14-2-18
    3.         Dim n0, n1, a0 As Integer 'number 0 and number 1 and answer
    4.         Dim f, add, subtract, multiply, divide As String 'function
    5.         Console.WriteLine("This programme will check which number is larger")
    6.         n0 = InputBox("please enter the first number")
    7.         Console.WriteLine("You have entered " & n0)
    8.         n1 = InputBox("please enter the second number")
    9.         Console.WriteLine("You have entered " & n1)
    10.         f = InputBox("Would you like to add, subtract, divide or multiply?")
    11.         Console.WriteLine("" & f)
    12.         Console.ReadLine()
    13.  
    14.         If f = add Then
    15.             a0 = (n0 + n1)
    16.             Console.WriteLine(" " & a0)
    17.         ElseIf f = subtract Then
    18.             a0 = (n0 - n1)
    19.             Console.WriteLine(" " & a0)
    20.         ElseIf f = multiply Then
    21.             a0 = (n0 * n1)
    22.             Console.WriteLine(" " & a0)
    23.         ElseIf f = divide Then
    24.             a0 = (n0 / n1)
    25.                         Console.WriteLine(" " & a0)
    26.                     End If
    27.                     Console.ReadLine()
    28.     End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,420

    Re: VB calculator functions

    For a start, this is a Console app so don't use InputBox. You shouldn't really use InputBox even in a WinForms app but it is often used for simplicity in learning exercises. For user input in a Console app, call Console.ReadLine.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,539

    Re: VB calculator functions

    Since you can use an InputBox from a Console application, it is probably up to the instructor whether they teach using it or not for user input. Using ReadLine would be more traditional, but who knows what they're teaching.

  6. #6

    Thread Starter
    Registered User
    Join Date
    Feb 2018
    Posts
    2

    Re: VB calculator functions

    So I honestly couldn't figure out how to fix, I appreciate that people showed me where, just leads me to the conclusion that I am going to struggle with this course, but I got there in the end by removing words(add etc) and replacing with numbers. Sorry about the readability issues, got rid of inputbox.
    Was the issue f = *a word*(string) but then being used as an integer?

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,014

    Re: VB calculator functions

    No, but that is a sensible thing to think about (it doesn't apply here, but might in other code you write).

    The issue is related to the actual values, so slightly expanding the previous hint, think about these lines of the code:
    Code:
            Console.WriteLine("" & f)
            Console.ReadLine()
     
            If f = add Then
    The first of those lines shows you the value of f, what is the value of add?

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,539

    Re: VB calculator functions

    I'll go ahead and be explicit.

    You created variables of type string, e.g. f, add, etc...
    So f and add are variables that can hold strings, they are not strings themselves.

    You can assign a string to the variable like this
    f = "Hello"
    add = "Bye"

    Now if you do... Console.WriteLine(f), it will print "Hello", not "f".
    Likewise, if you do... Console.WriteLine(add), it will print "Bye", not "add".

    When you write an If condition,
    If f = add Then

    You're not comparing "f" to "add", you would be comparing "Hello" to "Bye".

    In your code, you didn't set the variable, add, to anything, so if the user typed in "add", you are comparing to see whether f = add, i.e. whether "add" = "", which it doesn't.

    You probably wanted to compare to a literal string, i.e. an explicit string value not assigned to a variable which is a set of characters enclosed between double quotes.

    If f = "add" Then

    By changing to using numbers, you "fixed" the issue because Visual Basic will implicitly convert a string to a number by default, if it can.
    The literal for a number is just the number, you don't put quotes around it because that would make it a literal string.

    So, assuming your enumeration for the option "add" is 1, then your code

    If f = 1 Then

    is really doing

    If "1" = 1 Then

    because the user input is into a string variable.
    VB will convert "1" to 1 so the If condition will match.
    You could have written

    If f = "1" Then

    and it would still have worked. In this case you would be comparing a string input to a string so VB wouldn't have to convert the strings to numbers and the actual comparison would be comparing two strings...

    If "1" = "1" Then

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,395

    Re: VB calculator functions

    So you need to prompt for three values, the left/right operand and the operator. Since the operands will be numeric, you can use the TryParse method to validate that the user input a valid numeric value. Take a look at this example that will continue to loop until the user enters a valid Integer:
    Code:
    'Prompt for the first user input by looping until the user enters a valid Integer
    Console.Write("Input: ")
    Do Until Integer.TryParse(Console.ReadLine(), operand1)
        Console.WriteLine("The input was not in the correct format for an Integer.")
        Console.Write("Input: ")
    Loop
    The operators is a little bit different in that there is no built in method. One option that you have is to declare all the possible symbol characters, use Console.ReadKey to get the KeyChar that was entered, and then use IndexOf on the variable that holds all of the operators to compare if the KeyChar is found. Take a look at this example that will continue to loop until the user enters a valid operator symbol:
    Code:
    'Declare the valid operator symbols
    Dim operators As String = "+-*/"
    
    'Prompt for the operator by looping until the user enters a valid symbol
    Console.Write("Operator: ")
    symbol = Console.ReadKey.KeyChar
    Do Until operators.IndexOf(symbol) <> -1
        Console.WriteLine("Please input a +, -, *, or / symbol.")
        Console.Write("Operator: ")
        symbol = Console.ReadKey.KeyChar
    Loop
    Console.WriteLine()
    The last bit is to use a conditional If/Then statement to check which operation is to be done based on the operator symbol the user input. Take a look at this example which performs the respective operation on the two operands and then displays the result:
    Code:
    'Perform the respective operation on the two operands
    If symbol = "+"c Then
        result = operand1 + operand2
        Console.Write("{0} + {1} = ", operand1, operand2)
    ElseIf symbol = "-"c Then
        result = operand1 - operand2
        Console.Write("{0} - {1} = ", operand1, operand2)
    ElseIf symbol = "*"c Then
        result = operand1 * operand2
        Console.Write("{0} * {1} = ", operand1, operand2)
    Else
        result = Convert.ToInt32(operand1 / operand2)
        Console.Write("{0} / {1} = ", operand1, operand2)
    End If
    
    'Display the result
    Console.WriteLine(result)
    Console.ReadLine()
    Whenever it is all said and done, here is a full example:
    Code:
    'Declare the three variables, two to hold user input and one to display the result
    Dim operand1, operand2, result As Integer
    Dim symbol As Char
    
    'Declare the valid operator symbols
    Dim operators As String = "+-*/"
    
    'Prompt for the first user input by looping until the user enters a valid Integer
    Console.Write("Input: ")
    Do Until Integer.TryParse(Console.ReadLine(), operand1)
        Console.WriteLine("The input was not in the correct format for an Integer.")
        Console.Write("Input: ")
    Loop
    
    'Prompt for the operator by looping until the user enters a valid symbol
    Console.Write("Operator: ")
    symbol = Console.ReadKey.KeyChar
    Do Until operators.IndexOf(symbol) <> -1
        Console.WriteLine("Please input a +, -, *, or / symbol.")
        Console.Write("Operator: ")
        symbol = Console.ReadKey.KeyChar
    Loop
    Console.WriteLine()
    
    'Prompt for the next user input by looping until the user enters a valid Integer
    Console.Write("Input: ")
    Do Until Integer.TryParse(Console.ReadLine(), operand2)
        Console.WriteLine("The input was not in the correct format for an Integer.")
        Console.Write("Input: ")
    Loop
    
    'Perform the respective operation on the two operands
    If symbol = "+"c Then
        result = operand1 + operand2
        Console.Write("{0} + {1} = ", operand1, operand2)
    ElseIf symbol = "-"c Then
        result = operand1 - operand2
        Console.Write("{0} - {1} = ", operand1, operand2)
    ElseIf symbol = "*"c Then
        result = operand1 * operand2
        Console.Write("{0} * {1} = ", operand1, operand2)
    Else
        result = Convert.ToInt32(operand1 / operand2)
        Console.Write("{0} / {1} = ", operand1, operand2)
    End If
    
    'Display the result
    Console.WriteLine(result)
    Console.ReadLine()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width