Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Hi I am Writing a calculator in Visual basics and there seems to be a Error occurring.

+5 votes
asked Sep 9, 2023 by Ibrahim Ahmad (170 points)
Dim a,b,c As Integer
Dim op As String
Console.Writeline("Calculater")
    Console.Write ("Enter first number")
a=Console.Readline
    Console.write ("Enter second numbers")
b=Console.Readline
    Console.Write ("Enter Operator")
op=Console.Readline
If op = "t" Then
    c = a+b
ElseIf op = "-" Then
    c = a-b
ElseIf op = "*" Then
    c = a*b
ElseIf op = "/" Then
    c = a/b
ElseIf
     Console.WriteLine ("Invalid op")
End If
     Console.Write ("Answer="&c)
End Sub

2 Answers

0 votes
answered Sep 10, 2023 by Twisha Bose (140 points)
Dim a, b, c As Integer
Dim op As String

Console.WriteLine("Calculator")
Console.Write("Enter first number: ")
a = Convert.ToInt32(Console.ReadLine())

Console.Write("Enter second number: ")
b = Convert.ToInt32(Console.ReadLine())

Console.Write("Enter Operator: ")
op = Console.ReadLine()

If op = "+" Then
    c = a + b
ElseIf op = "-" Then
    c = a - b
ElseIf op = "*" Then
    c = a * b
ElseIf op = "/" Then
    If b = 0 Then
        Console.WriteLine("Division by zero is not allowed")
    Else
        c = a / b
    End If
Else
    Console.WriteLine("Invalid operator")
End If

Console.WriteLine("Answer = " & c)
0 votes
answered Nov 18, 2023 by Gulshan Negi (1,580 points)

Well, there are some syntax error with your code, can you try below code, I am sure it may fix the error you are getting.

Module Module1
    Sub Main()
        Dim a, b, c As Integer
        Dim op As String

        Console.WriteLine("Calculator")
        Console.Write("Enter first number: ")
        a = Console.ReadLine() ' Use ReadLine instead of Readline

        Console.Write("Enter second number: ")
        b = Console.ReadLine() ' Use ReadLine instead of Readline

        Console.Write("Enter Operator: ")
        op = Console.ReadLine() ' Use ReadLine instead of Readline

        If op = "+" Then ' Corrected the operator for addition
            c = a + b
        ElseIf op = "-" Then
            c = a - b
        ElseIf op = "*" Then
            c = a * b
        ElseIf op = "/" Then
            If b <> 0 Then
                c = a / b
            Else
                Console.WriteLine("Cannot divide by zero")
                Exit Sub ' Exit the program if division by zero is attempted
            End If
        Else
            Console.WriteLine("Invalid operator")
            Exit Sub ' Exit the program for invalid operators
        End If

        Console.WriteLine("Answer = " & c)
    End Sub
End Module

Thanks

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...