6

I was trying to make a calculator in VBScript to test my skill, but came across an error. My program uses multiple else if statement to test what the user has input.

Here is my code below:

Dim head
Dim msg1, msgErr, msgAns
Dim input1, num1, num2
Dim ans

head = "Calculator"

msg1 = msgBox("Ruan's Vbscript calculator",0,head)
input1 = inputBox("How do you want to calculate? You can type (+ - * /)",head)

num1 = inputBox("Enter your first number",head)
num2 = inputBox("Enter your second number",head)

if (input1 = vbcancel) then
    wscript.quit()
else if (input1 = "+") then
    ans = num1 + num2
else if (input1 = "-") then
    ans = num1 - num2
else if (input1 = "*") then
    ans = num1 * num2
else if (input1 = "/") then
    ans = num1 / num2
else
    msgErr = msgBox("Make sure you type '+' or '-' or '*' or '/' with no extra letter or spaces","Error")
end if

msgAns = msgBox "Your answere is: " + head

When I run the program, the error says: "Expected end of statement". I don't see the problem here, as I have end if after all those else if statements.

3
  • I believe the correct syntax in VBScript is ElseIf w3schools.com/vbscript/vbscript_conditionals.asp Commented Jun 1, 2015 at 11:49
  • input1 = vbcancel does not work as some may think. Commented Jun 1, 2015 at 12:02
  • 2
    Any of the upvoters willing to explain why a calculator that claims that 5+7 is 57 deserves points? Commented Jun 1, 2015 at 12:22

3 Answers 3

5

Remove the space between else and if and make it elseif. Like this:

if (input1 = vbcancel) then
    wscript.quit()
elseif (input1 = "+") then
    ans = num1 + num2
elseif (input1 = "-") then
    ans = num1 - num2
elseif (input1 = "*") then
    ans = num1 * num2
elseif (input1 = "/") then
    ans = num1 / num2
else
    msgErr = msgBox("Make sure you type '+' or '-' or '*' or '/' with no extra letter or spaces","Error")
end if

With the additional space between else and if you start a new if statement nested in the else branch rather than continuing the first if statement. The nested if statements require end ifs of their own, which is why you're getting the error.

Sign up to request clarification or add additional context in comments.

1 Comment

input1 = vbcancel does not work as some may think.
2

Minimal improvements to make it work:

Option Explicit

Dim head
Dim msg1, msgErr, msgAns
Dim input1, num1, num2
Dim ans

head = "Calculator"
msgBox "Ruan's Vbscript calculator", 0, head
input1 = inputBox("How do you want to calculate? You can type (+ - * /)",head)
If Not IsEmpty(input1) Then
   num1 = CDbl(inputBox("Enter your first number",head))
   num2 = CDbl(inputBox("Enter your second number",head))
   if     input1 = "+" then
      ans = num1 + num2
   elseif input1 = "-" then
      ans = num1 - num2
   elseif input1 = "*" then
      ans = num1 * num2
   elseif input1 = "/" then
      ans = num1 / num2
   elseif input1 = "\" then
      ans = num1 \ num2
   else
      msgErr = msgBox("Make sure you type '+' or '-' or '*' or '/' with no extra letter or spaces","Error")
   end if
   msgBox "Your answere is: " & ans
else
   msgBox "Aborted"
end if

Type and range checks for the operants left as execise.

Comments

0

A Select Case is usually a simpler/clearer construction for selecting operations based on a single variables value

Select Case input1
    Case vbCancel
        wscript.quit()
    Case "+"
        ans = num1 + num2
    Case "-"
        ans = num1 - num2
    Case "*"
        ans = num1 * num2
    Case "/", "\" '//multiples
        ans = num1 / num2
    Case Else
        msgBox "Make sure you type '+' or '-' or '*' or '/' with no extra letter or spaces", , "Error"
End Select

2 Comments

Please reconsider Case "/", "\" '//multiples (misleading comment, real vs integer div).
input1 = vbcancel does not work as some may think.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.