Reputation: 157
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.
Upvotes: 6
Views: 8749
Reputation: 38745
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.
Upvotes: 2
Reputation: 172398
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 if
s of their own, which is why you're getting the error.
Upvotes: 5
Reputation: 175768
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
Upvotes: 0